Merge origin/main into fix/objectscript-v1.11.0-1261

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
This commit is contained in:
Martin Vogel
2026-11-19 18:58:24 +01:00
62 changed files with 6603 additions and 1027 deletions
+36 -6
View File
@@ -429,7 +429,8 @@ EXTRACTION_SRCS = \
$(CBM_DIR)/iris_export_xml.c \
$(CBM_DIR)/service_patterns.c \
$(CBM_DIR)/spawn_patterns.c \
$(CBM_DIR)/resolve_stats.c
$(CBM_DIR)/resolve_stats.c \
$(CBM_DIR)/extract_scope.c
# LSP resolvers (compiled as one unit via lsp_all.c)
LSP_SRCS = $(CBM_DIR)/lsp_all.c
@@ -576,7 +577,8 @@ PIPELINE_SRCS = \
src/pipeline/pass_cross_repo.c \
src/pipeline/artifact.c \
src/pipeline/pass_ensemble_routing.c \
src/pipeline/pass_pkgmap.c
src/pipeline/pass_pkgmap.c \
src/pipeline/site_bind.c
# SimHash / MinHash module
SIMHASH_SRCS = src/simhash/minhash.c
@@ -849,7 +851,8 @@ TEST_DAEMON_SRCS = \
tests/test_daemon_application.c \
tests/test_daemon_frontend.c \
tests/test_daemon_bootstrap.c \
tests/test_daemon_ipc.c
tests/test_daemon_ipc.c \
tests/image_probe_roles.c
TEST_DISCOVER_SRCS = \
tests/test_language.c \
@@ -871,7 +874,7 @@ TEST_DISCOVER_SRCS = \
TEST_GRAPH_BUFFER_SRCS = tests/test_graph_buffer.c
TEST_PIPELINE_SRCS = tests/test_registry.c tests/test_pipeline.c tests/test_pipeline_conventions.c tests/test_pipeline_frozen.c tests/test_importance.c tests/test_cross_repo.c tests/test_fqn.c tests/test_route_canon.c tests/test_path_alias.c tests/test_configlink.c tests/test_doclinks.c tests/test_doc_mentions.c tests/test_doc_links_md.c tests/test_doc_links_pdf.c tests/test_doc_links_rst.c tests/test_doc_links_adoc.c tests/test_doc_mentions_go.c tests/test_doc_mentions_jvm.c tests/test_doc_mentions_rs.c tests/test_doc_mentions_perl.c tests/test_doc_mentions_ts.c tests/test_doc_mentions_php.c tests/test_doc_mentions_c.c tests/test_diagrams.c tests/test_openapi.c tests/test_infrascan.c tests/test_worker_pool.c tests/test_parallel.c tests/test_type_scope.c tests/test_index_resilience.c tests/test_index_format.c tests/test_call_reference_contract.c tests/repro/repro_call_scope_usages.c tests/repro/repro_call_argument_usages.c tests/repro/repro_reference_precision.c tests/repro/repro_lexical_binding_precision.c tests/repro/repro_call_argument_matrix_a.c tests/repro/repro_call_argument_matrix_b.c tests/repro/repro_call_node_behaviors.c tests/repro/repro_language_registry.c tests/repro/repro_call_node_manifest.c tests/repro/repro_lsp_ordered_signatures.c tests/repro/repro_lsp_ordered_local.c tests/repro/repro_ts_overload_return_chains.c tests/repro/repro_harness_cleanup.c tests/repro/repro_runner_filter.c
TEST_PIPELINE_SRCS = tests/test_registry.c tests/test_pipeline.c tests/test_pipeline_conventions.c tests/test_pipeline_frozen.c tests/test_importance.c tests/test_cross_repo.c tests/test_fqn.c tests/test_route_canon.c tests/test_path_alias.c tests/test_configlink.c tests/test_doclinks.c tests/test_doc_mentions.c tests/test_doc_links_md.c tests/test_doc_links_pdf.c tests/test_doc_links_rst.c tests/test_doc_links_adoc.c tests/test_doc_mentions_go.c tests/test_doc_mentions_jvm.c tests/test_doc_mentions_rs.c tests/test_doc_mentions_perl.c tests/test_doc_mentions_ts.c tests/test_doc_mentions_php.c tests/test_doc_mentions_c.c tests/test_diagrams.c tests/test_openapi.c tests/test_infrascan.c tests/test_worker_pool.c tests/test_parallel.c tests/test_type_scope.c tests/test_index_resilience.c tests/test_index_format.c tests/test_call_reference_contract.c tests/repro/repro_call_scope_usages.c tests/repro/repro_call_argument_usages.c tests/repro/repro_reference_precision.c tests/repro/repro_lexical_binding_precision.c tests/repro/repro_call_argument_matrix_a.c tests/repro/repro_call_argument_matrix_b.c tests/repro/repro_call_node_behaviors.c tests/repro/repro_language_registry.c tests/repro/repro_call_node_manifest.c tests/repro/repro_lsp_ordered_signatures.c tests/repro/repro_lsp_ordered_local.c tests/repro/repro_ts_overload_return_chains.c tests/repro/repro_harness_cleanup.c tests/repro/repro_runner_filter.c tests/test_site_bind.c tests/test_scope_facts.c
TEST_WATCHER_SRCS = tests/test_watcher.c tests/test_svn_state.c
@@ -1214,7 +1217,34 @@ $(UNIXCODER_OBJ): $(UNIXCODER_BLOB_SRC) vendored/nomic/code_vectors.bin | $(BUIL
OBJS_VENDORED_TEST = $(MIMALLOC_OBJ_TEST) $(SQLITE3_OBJ_TEST) $(TRE_OBJ_TEST) $(GRAMMAR_OBJS_TEST) $(TS_RUNTIME_OBJ_TEST) $(LSP_OBJ_TEST) $(PP_OBJ_TEST) $(LZ4_OBJ_TEST) $(ZSTD_OBJ_TEST) $(UNIXCODER_OBJ)
OBJS_VENDORED_TSAN = $(MIMALLOC_OBJ_TSAN) $(SQLITE3_OBJ_TSAN) $(TRE_OBJ_TSAN) $(GRAMMAR_OBJS_TSAN) $(TS_RUNTIME_OBJ_TSAN) $(LSP_OBJ_TSAN) $(PP_OBJ_TSAN) $(LZ4_OBJ_TSAN) $(ZSTD_OBJ_TSAN) $(UNIXCODER_OBJ)
$(BUILD_DIR)/test-runner: $(ALL_TEST_SRCS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_TEST) $(PROJECT_HDRS) | $(BUILD_DIR)
# ── Image probe (tests/test_image_probe.h) ──────────────────────
# The small executable that copied-image and install tests run or publish
# instead of a copy of the runner: the daemon runtime, foundation and the probe
# roles, ~5 MB against ~800 MB for the sanitized runner, every full SHA-256 of
# which costs 70-95 s on some hosted x86 CPUs. One beside each runner flavour,
# built with that flavour's flags and found by the runner's own name
# (<runner>-image-probe), so every venue that builds a runner builds its probe:
# each runner rule lists its probe as an order-only prerequisite. Coverage
# instrumentation stays out: the probe's profile must never land in the
# per-test profile of the runner that started it.
# workspace.c (needs mcp.c's path check) and slab_alloc.c (needs the tree-sitter
# runtime) serve the indexer, not the daemon runtime, so the probe leaves them out.
IMAGE_PROBE_FOUNDATION_SRCS = $(filter-out src/foundation/workspace.c src/foundation/slab_alloc.c,$(FOUNDATION_SRCS))
IMAGE_PROBE_SRCS = tests/image_probe_main.c tests/image_probe_roles.c \
src/daemon/daemon.c src/daemon/ipc.c src/daemon/runtime.c \
src/daemon/service.c $(IMAGE_PROBE_FOUNDATION_SRCS)
CFLAGS_IMAGE_PROBE = $(filter-out $(TEST_COVERAGE_FLAGS) $(TEST_COVERAGE_DEFINE),$(CFLAGS_TEST))
# TRE_OBJ_* is empty except on Windows, where compat_regex.c is backed by TRE.
$(BUILD_DIR)/test-runner-image-probe: $(IMAGE_PROBE_SRCS) $(MIMALLOC_OBJ_TEST) $(TRE_OBJ_TEST) $(PROJECT_HDRS) | $(BUILD_DIR)
$(CC) $(CFLAGS_IMAGE_PROBE) -Itests -o $@ $(IMAGE_PROBE_SRCS) $(MIMALLOC_OBJ_TEST) \
$(TRE_OBJ_TEST) $(LDFLAGS_TEST)
$(BUILD_DIR)/test-runner-tsan-image-probe: $(IMAGE_PROBE_SRCS) $(MIMALLOC_OBJ_TSAN) $(TRE_OBJ_TSAN) $(PROJECT_HDRS) | $(BUILD_DIR)
$(CC) $(CFLAGS_TSAN) -Itests -o $@ $(IMAGE_PROBE_SRCS) $(MIMALLOC_OBJ_TSAN) $(TRE_OBJ_TSAN) \
$(LDFLAGS_TSAN)
$(BUILD_DIR)/test-runner: $(ALL_TEST_SRCS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_TEST) $(PROJECT_HDRS) | $(BUILD_DIR) $(BUILD_DIR)/test-runner-image-probe
$(CC) $(CFLAGS_TEST) -Itests -Itests/repro -o $@ \
$(ALL_TEST_SRCS) $(PROD_SRCS) \
$(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) \
@@ -1302,7 +1332,7 @@ TSAN_OPTIONS ?= halt_on_error=1:report_thread_leaks=0
# Normal ASan/native/soak paths remain uncapped. High-worker TSan diagnostics
# invoke test-runner-tsan directly so release gates cannot drift accidentally.
$(BUILD_DIR)/test-runner-tsan: $(ALL_TEST_SRCS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_TSAN) $(PROJECT_HDRS) | $(BUILD_DIR)
$(BUILD_DIR)/test-runner-tsan: $(ALL_TEST_SRCS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_TSAN) $(PROJECT_HDRS) | $(BUILD_DIR) $(BUILD_DIR)/test-runner-tsan-image-probe
$(CC) $(CFLAGS_TSAN) -Itests -Itests/repro -o $@ \
$(ALL_TEST_SRCS) $(PROD_SRCS) \
$(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) \
+14 -2
View File
@@ -75,8 +75,20 @@ For directories:
suffix instead: Laravel's compiled Blade view cache
`storage/framework/views/` (at any depth) is skipped even without
Laravel's stock `.gitignore` there, while other `views/` folders stay
indexed. A `.cbmignore` negation
(for example `!target/`) can un-skip these directories, except the
indexed. Two names need more than the name: a folder called `env` is
skipped only when it is a Python environment (it holds `pyvenv.cfg`,
`bin/activate`, `Scripts/activate` or `conda-meta/history`, matched by
exact name), and `deploy/` and `deployed/` are walked for their
deployment and configuration files only. Kept there: Makefile, CMake,
YAML, TOML, JSON, INI, Dockerfile, Properties, XML, JSON5, Jsonnet,
Jinja2, Kustomize, Go templates, HCL (Terraform, OpenTofu, Packer), Bicep,
Nix, Puppet, Pkl, `.env` files, and Bash, Zsh, Fish and PowerShell
scripts. Skipped there: program source of every other language, SQL,
Groovy, and package manifests (`package.json`, `go.mod`, `Cargo.toml`,
`pyproject.toml`, `pom.xml`, ...). A large generated folder under these
names costs what it costs under any other name; exclude one with a
`.cbmignore` line such as `deploy/generated/`. A `.cbmignore` negation
(for example `!target/` or `!deploy/`) can un-skip these directories, except the
non-negatable safety core: `.git`, `node_modules`, `.worktrees`, and
`.claude-worktrees`.
2. **Repo `.gitignore`** — `<repo>/.gitignore` merged with
+91 -12
View File
@@ -28,6 +28,7 @@
#include "cpp_branch_views.h"
#include "foundation/compat.h"
#include "foundation/compat_fs.h" // cbm_fopen — crash-supervisor per-file marker write
#include "foundation/limits.h" // cbm_max_file_bytes — headers read by the #include lookup
#include "foundation/hash_table.h" // CBMHashTable — crash-supervisor quarantine set
#include "foundation/str_util.h" // cbm_json_escape — variants file_path
#include "tree_sitter/api.h" // TSParser, TSNode, TSTree, TSInput, TSLanguage, TSPoint, TSParseOptions, TSParseState
@@ -404,6 +405,11 @@ void cbm_imports_push(CBMImportArray *arr, CBMArena *a, CBMImport imp) {
arr->items[arr->count++] = imp;
}
void cbm_scope_events_push(CBMScopeEventArray *arr, CBMArena *a, CBMScopeEvent ev) {
GROW_ARRAY(arr, a);
arr->items[arr->count++] = ev;
}
bool cbm_imports_local_span_binds_runtime_name(const CBMImportArray *imports, const char *name,
size_t len) {
if (!imports || !name) {
@@ -3823,14 +3829,81 @@ static void cbm_swift_type_imports(CBMFileResult *result, CBMLanguage language)
}
}
static CBMFileResult *extract_file_ex_body(const char *source, int source_len, CBMLanguage language,
const char *project, const char *rel_path,
int64_t timeout_micros, const char **extra_defines,
const char **include_paths,
const CBMMacroTable *macro_table,
const CBMReturnTypeTable *return_type_table,
CBMArena *scratch,
const cbm_test_declarations_t *test_declarations) {
/* ── #include lookup of the preprocessed pass (#2993) ──────────────
* Quoted #include names of the C-family second pass resolve against the
* indexed repository: preprocessor.cpp hands over a normalised, root-relative
* name, and the header is read only when it is one of the files the index run
* covers (CBMIncludeScope.is_indexed; discovery has already applied its rules:
* no symlinks or junctions, ignore files, skipped directories), within the
* per-file size cap. Every file the expansion depends on is therefore in the
* run's manifest. Paths are UTF-8, opened through cbm_fopen. */
static char *include_tree_read(const void *userdata, const char *rel_path, size_t *out_len) {
const CBMIncludeScope *scope = (const CBMIncludeScope *)userdata;
if (!rel_path[0] || !scope->is_indexed || !scope->is_indexed(scope->userdata, rel_path)) {
return NULL;
}
size_t root_len = strlen(scope->root);
bool sep =
root_len > 0 && scope->root[root_len - 1] != '/' && scope->root[root_len - 1] != '\\';
char path[CBM_SZ_4K];
int written = snprintf(path, sizeof(path), "%s%s%s", scope->root, sep ? "/" : "", rel_path);
if (written < 0 || (size_t)written >= sizeof(path)) {
return NULL;
}
FILE *f = cbm_fopen(path, "rb");
if (!f) {
return NULL;
}
long size = fseek(f, 0, SEEK_END) == 0 ? ftell(f) : -1;
if (size < 0 || size > cbm_max_file_bytes() || fseek(f, 0, SEEK_SET) != 0) {
(void)fclose(f);
return NULL;
}
char *data = (char *)cbm_alloc(CBM_MEM_CLASS_EXTRACT, (size_t)size + 1);
size_t got = data && size > 0 ? fread(data, 1, (size_t)size, f) : 0;
(void)fclose(f);
if (data) {
data[got] = '\0';
*out_len = got;
}
return data;
}
static void include_tree_release(const void *userdata, char *data) {
(void)userdata;
cbm_free(CBM_MEM_CLASS_EXTRACT, data);
}
/* result->included_files from what the preprocessed pass's lookup read. An
* allocation failure marks the result as failed: a missing list would let an
* incremental refresh skip this file after one of its headers changed. */
static void record_included_files(CBMArena *a, CBMFileResult *result,
const CBMPreprocessedSource *pp) {
if (!pp || pp->included_count <= 0) {
return;
}
const char **list =
(const char **)cbm_arena_alloc(a, ((size_t)pp->included_count + 1) * sizeof(char *));
for (int i = 0; list && i < pp->included_count; i++) {
list[i] = cbm_arena_strdup(a, pp->included_files[i]);
if (!list[i]) {
list = NULL;
}
}
if (!list) {
result->has_error = true;
return;
}
list[pp->included_count] = NULL;
result->included_files = list;
}
static CBMFileResult *extract_file_ex_body(
const char *source, int source_len, CBMLanguage language, const char *project,
const char *rel_path, int64_t timeout_micros, const char **extra_defines,
const char **include_paths, const CBMMacroTable *macro_table,
const CBMReturnTypeTable *return_type_table, CBMArena *scratch,
const cbm_test_declarations_t *test_declarations, const CBMIncludeScope *include_scope) {
// Allocate result on heap (arena inside for all string data)
CBMFileResult *result = cbm_result_alloc();
if (!result) {
@@ -4091,6 +4164,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
cbm_extract_imports(&ctx);
cbm_extract_python_namespace_facts(&ctx);
cbm_extract_unified(&ctx);
cbm_extract_scope(&ctx);
cbm_swift_type_imports(result, language);
if (cbm_lang_needs_cpp_branch_views(language)) {
extract_cpp_branch_views(&ctx, ts_lang, timeout_micros);
@@ -4223,8 +4297,12 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
char export_cands[CBM_EXPORT_MACRO_MAX][CBM_EXPORT_MACRO_NAME_MAX];
int export_cand_count =
cbm_export_macro_candidates(source, source_len, export_cands, CBM_EXPORT_MACRO_MAX);
CBMIncludeReader include_reader = {include_tree_read, include_tree_release, include_scope};
bool include_lookup = include_scope && include_scope->root && include_scope->root[0];
CBMPreprocessedSource *preprocessed = cbm_preprocess_with_map(
source, source_len, rel_path, extra_defines, include_paths, language != CBM_LANG_C);
source, source_len, rel_path, extra_defines, include_paths, language != CBM_LANG_C,
include_lookup ? &include_reader : NULL);
record_included_files(a, result, preprocessed);
if (preprocessed && preprocessed->source) {
char *expanded = preprocessed->source;
int expanded_len = (int)strlen(expanded);
@@ -4893,14 +4971,15 @@ CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLangua
const CBMReturnTypeTable *return_type_table) {
return cbm_extract_file_ex_with_tests(source, source_len, language, project, rel_path,
timeout_micros, extra_defines, include_paths, macro_table,
return_type_table, NULL);
return_type_table, NULL, NULL);
}
CBMFileResult *cbm_extract_file_ex_with_tests(
const char *source, int source_len, CBMLanguage language, const char *project,
const char *rel_path, int64_t timeout_micros, const char **extra_defines,
const char **include_paths, const CBMMacroTable *macro_table,
const CBMReturnTypeTable *return_type_table, const cbm_test_declarations_t *test_declarations) {
const CBMReturnTypeTable *return_type_table, const cbm_test_declarations_t *test_declarations,
const CBMIncludeScope *include_scope) {
CBMArena scratch;
if (tl_scratch_live && tl_scratch_slot) {
scratch = *tl_scratch_slot;
@@ -4911,7 +4990,7 @@ CBMFileResult *cbm_extract_file_ex_with_tests(
}
CBMFileResult *result = extract_file_ex_body(
source, source_len, language, project, rel_path, timeout_micros, extra_defines,
include_paths, macro_table, return_type_table, &scratch, test_declarations);
include_paths, macro_table, return_type_table, &scratch, test_declarations, include_scope);
/* Even early parse/quarantine exits publish conservative Python metadata.
* No tree is reparsed, and existing best-effort extraction flags stay intact. */
if (result && language == CBM_LANG_PYTHON &&
+81 -2
View File
@@ -478,6 +478,11 @@ typedef struct {
* re-export, bare `require('x').y`), so local_name is the path's last segment,
* not a binding the file can call through (#1128). Default false. */
bool local_name_synthesized;
/* Byte offset of the import statement in the source, recorded by the
* spec-driven import parser (parse_spec_imports); 0 where no parser sets
* it. Scope facts (CBMScopeEvent INCLUDE) find their row by it. Fills
* padding: the struct does not grow. */
uint32_t start_byte;
const char *local_name; // local alias or name
const char *module_path; // resolved module path / QN
const char *member; // ES named import: the exported name the local binds (`get` in
@@ -756,6 +761,48 @@ typedef struct {
int cap;
} CBMImportArray;
/* Scope facts (extract_scope.c): what one file defines and includes, in
* source order, for the languages with a scope row. A pure function of the
* file's bytes; empty for every other language. The values are those of the
* parked include-scope branch; the gaps are its ordered-row kinds (shell,
* Make), which keep their numbers when they come. */
typedef enum {
CBM_SCOPE_DEF = 1, /* result->defs[index] is defined here */
CBM_SCOPE_INCLUDE = 2, /* result->imports[index] takes effect after `pos` */
CBM_SCOPE_ASSIGN = 3, /* `name` is defined here by a form the graph has no node for */
/* bytes [index, pos) are a qualified type reference written as `name`,
* qualifier included ("google.protobuf.Empty", ".a.B"): a usage site of
* the reference carries only its last identifier */
CBM_SCOPE_QREF = 5,
/* a parse the facts cannot trust: what is found past it is not proven */
CBM_SCOPE_OPAQUE = 8,
} CBMScopeEventKind;
enum {
CBM_SCOPE_REEXPORT = 1u << 4, /* INCLUDE: its names reach this file's includers */
/* inside a parse error: may or may not hold where a later name is used */
CBM_SCOPE_CONDITIONAL = 1u << 5,
};
typedef struct {
const char *name; /* defined name, or QREF's text; NULL for INCLUDE and OPAQUE */
/* DEF: the full name where the row has packages or nesting (Protobuf:
* package, enclosing messages, name: "a.b.Outer.Inner"). INCLUDE: the
* importing file's package ("" when none), which its references are
* relative to. NULL elsewhere. */
const char *qual;
uint32_t pos; /* byte offset after which the event is in effect */
int32_t index; /* into defs (DEF) or imports (INCLUDE); QREF: start byte; else -1 */
uint8_t kind; /* CBMScopeEventKind */
uint8_t flags; /* CBM_SCOPE_* */
} CBMScopeEvent;
typedef struct {
CBMScopeEvent *items;
int count;
int cap;
} CBMScopeEventArray;
typedef struct {
CBMUsage *items;
int count;
@@ -967,6 +1014,11 @@ typedef struct CBMFileResult {
const char **constants; // NULL-terminated (NULL if none)
const char **global_vars; // NULL-terminated (NULL if none)
const char **macros; // NULL-terminated, C/C++ only (NULL if none)
/* C, C++, CUDA: the repository files the preprocessed pass's #include
* lookup read (repository-relative, sorted, NULL-terminated; NULL if none).
* The file's graph depends on their bytes as well as its own, so an
* incremental refresh re-extracts the file when one of them changes. */
const char **included_files;
bool has_error;
const char *error_msg;
@@ -1061,6 +1113,10 @@ typedef struct CBMFileResult {
* bytes; NULL for languages without a scope scanner. */
CBMDocLinkArray doc_links;
const char *doc_scope;
/* Scope facts: definitions and includes in source order (extract_scope.c);
* empty outside the languages with a scope row. */
CBMScopeEventArray scope;
} CBMFileResult;
// --- Enclosing function cache ---
@@ -1315,15 +1371,29 @@ CBMFileResult *cbm_extract_file_ex(
const CBMReturnTypeTable *return_type_table // OS return types, or NULL
);
/* The indexed repository as the C-family preprocessed pass's #include lookup
* sees it (#2993): `root` is the directory rel_path is relative to, and a
* quoted include resolves only to a file is_indexed() accepts -- one of the
* files the index run covers -- so the expansion is a function of the indexed
* tree alone, never of the process working directory. */
typedef struct {
const char *root;
bool (*is_indexed)(const void *userdata, const char *rel_path);
const void *userdata;
} CBMIncludeScope;
/* Additive immutable-snapshot entry point. NULL preserves legacy behavior.
* Snapshot must outlive this call; returned data is independently result-owned.
* A non-OK result status is an explicit configured-mapping failure, not a
* successful empty inventory. error_msg may be NULL on diagnostic OOM. */
* successful empty inventory. error_msg may be NULL on diagnostic OOM.
* include_scope: where quoted #include names resolve (preprocessor.h); NULL
* resolves none. */
CBMFileResult *cbm_extract_file_ex_with_tests(
const char *source, int source_len, CBMLanguage language, const char *project,
const char *rel_path, int64_t timeout_micros, const char **extra_defines,
const char **include_paths, const CBMMacroTable *macro_table,
const CBMReturnTypeTable *return_type_table, const cbm_test_declarations_t *test_declarations);
const CBMReturnTypeTable *return_type_table, const cbm_test_declarations_t *test_declarations,
const CBMIncludeScope *include_scope);
// Free all memory associated with a result.
void cbm_free_result(CBMFileResult *result);
@@ -1432,6 +1502,7 @@ uint64_t cbm_extract_test_go_embedded_work(void);
void cbm_defs_push(CBMDefArray *arr, CBMArena *a, CBMDefinition def);
void cbm_calls_push(CBMCallArray *arr, CBMArena *a, CBMCall call);
void cbm_imports_push(CBMImportArray *arr, CBMArena *a, CBMImport imp);
void cbm_scope_events_push(CBMScopeEventArray *arr, CBMArena *a, CBMScopeEvent ev);
/* True unless every import of `imports` that binds `local_name` is
* annotation-only (cbm_import_binds_runtime_name), so also true when no import
* names it: the rule for the builders that key IMPORTS edges by local name. */
@@ -1522,6 +1593,14 @@ const char *cbm_route_canon_path(const char *in, char *out, size_t out_sz);
// Single-pass unified extraction (replaces the 7 calls above except defs+imports).
void cbm_extract_unified(CBMExtractCtx *ctx);
/* Scope facts (extract_scope.c): result->scope for a language with a scope
* row, after its definitions and imports; nothing for any other language. */
void cbm_extract_scope(CBMExtractCtx *ctx);
/* True for a language with a scope row. */
bool cbm_extract_scope_has_row(CBMLanguage lang);
/* Nodes visited and index probes of every scope walk so far (growth tests). */
uint64_t cbm_extract_scope_nodes_visited(void);
// K8s / Kustomize semantic extractor (called when language is CBM_LANG_K8S or CBM_LANG_KUSTOMIZE).
void cbm_extract_k8s(CBMExtractCtx *ctx);
+5
View File
@@ -3028,9 +3028,14 @@ static void parse_spec_imports(CBMExtractCtx *ctx) {
if (!spec_type_matches(spec->import_node_types, kind)) {
continue;
}
int first = ctx->result->imports.count;
if (!try_generic_path_fields(ctx, node)) {
generic_import_from_text(ctx, node);
}
/* the statement every row came from (CBMImport.start_byte) */
for (int i = first; i < ctx->result->imports.count; i++) {
ctx->result->imports.items[i].start_byte = ts_node_start_byte(node);
}
} while (ts_tree_cursor_goto_next_sibling(&cursor));
ts_tree_cursor_delete(&cursor);
}
+465
View File
@@ -0,0 +1,465 @@
/*
* extract_scope.c — what one file defines and includes, in source order: the
* per-file facts of the shared cross-file layer (cbm.h, CBMScopeEvent).
*
* One walk, the same for every language with a row below. A row names syntax
* only: which node kinds define a type (and the label of the definition the
* extractor made for it), which are include statements, which are qualified
* type references, which statement names the file's package, and which type
* kind names the types nested in it. A definition event is the definition
* result->defs already holds (same start line, name and label); a
* definition-shaped node without one is an ASSIGN (a name defined here that
* the graph has no node for). Include events are result->imports, matched by
* the statement's start byte (the import parser records it). Every event
* takes effect after its statement: its position is the node's end byte. A
* parse error is an OPAQUE event, and what it holds is CONDITIONAL. Nothing
* is resolved here.
*
* Lifted from the parked include-scope branch (round 5): its neutral walk and
* its Protobuf handlers. The shell and Make handlers and their event kinds
* come with their own rows.
*/
#include "cbm.h"
#include "arena.h"
#include "helpers.h"
#include "tree_sitter/api.h"
#include "foundation/constants.h"
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
SC_ROLE_NONE = 0,
SC_ROLE_TYPE, /* defines a type (Protobuf message, enum) */
SC_ROLE_INCLUDE, /* an include statement, when an import starts here */
SC_ROLE_QREF, /* a qualified type reference: its text, qualifier included */
} sc_role_t;
typedef struct {
const char *kind;
sc_role_t role;
const char *label; /* TYPE: the label of the definition the extractor makes for it */
} sc_rule_t;
typedef struct {
CBMLanguage lang;
const sc_rule_t *rules;
const char *const *name_kinds; /* the kinds of the child naming a definition */
const char *package_kind; /* the statement naming the file's package; NULL: none */
const char *nest_kind; /* a type kind whose nested types it names; NULL: none */
bool public_reexports; /* `import public` re-exports what it imports */
} sc_lang_t;
static const sc_rule_t SC_PROTO_RULES[] = {
{"message", SC_ROLE_TYPE, "Class"}, {"enum", SC_ROLE_TYPE, "Class"},
{"import", SC_ROLE_INCLUDE, NULL}, {"message_or_enum_type", SC_ROLE_QREF, NULL},
{NULL, SC_ROLE_NONE, NULL},
};
static const char *const SC_PROTO_NAMES[] = {"message_name", "enum_name", NULL};
static const sc_lang_t SC_LANGS[] = {
{CBM_LANG_PROTOBUF, SC_PROTO_RULES, SC_PROTO_NAMES, "package", "message", true},
};
static _Atomic uint64_t g_scope_nodes_visited = 0;
uint64_t cbm_extract_scope_nodes_visited(void) {
return atomic_load_explicit(&g_scope_nodes_visited, memory_order_relaxed);
}
static const sc_lang_t *sc_lang(CBMLanguage lang) {
for (size_t i = 0; i < sizeof(SC_LANGS) / sizeof(SC_LANGS[0]); i++) {
if (SC_LANGS[i].lang == lang) {
return &SC_LANGS[i];
}
}
return NULL;
}
bool cbm_extract_scope_has_row(CBMLanguage lang) {
return sc_lang(lang) != NULL;
}
/* ── Definitions by start line, imports by start byte ─────────────── */
typedef struct {
uint32_t key; /* a definition's start line, an import's start byte */
int index;
} sc_ref_t;
static int sc_ref_cmp(const void *a, const void *b) {
const sc_ref_t *x = a;
const sc_ref_t *y = b;
if (x->key != y->key) {
return x->key < y->key ? -1 : 1;
}
return (x->index > y->index) - (x->index < y->index);
}
/* The first entry of refs[0..n) whose key is at least `key`. */
static int sc_lower_bound(const sc_ref_t *refs, int n, uint32_t key, uint64_t *steps) {
int lo = 0;
int hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / PAIR_LEN;
(*steps)++;
if (refs[mid].key < key) {
lo = mid + SKIP_ONE;
} else {
hi = mid;
}
}
return lo;
}
typedef struct {
CBMExtractCtx *ctx;
const sc_lang_t *lang;
sc_ref_t *defs; /* result->defs by start line */
int ndefs;
sc_ref_t *imps; /* result->imports with a module path, by start byte */
int nimps;
const char *pkg; /* the file's package ("" when none) */
uint32_t depth; /* the cursor depth of the node being visited */
uint64_t steps; /* index probes (growth tests read them with the nodes) */
/* the enclosing nesting types of the cursor, outermost first (their full
* names and depths): a nested type's full name in O(1) */
const char **nest_full;
uint32_t *nest_depth;
uint32_t nnest;
uint32_t cap_nest;
/* depths of the parse errors above the cursor: what they hold may or may
* not be what the source means */
uint32_t *cond_depth;
uint32_t ncond;
uint32_t cap_cond;
} sc_walk_t;
/* The definition of `label` named `name` that starts on `line`; -1 when there
* is none. */
static int sc_find_def(sc_walk_t *w, uint32_t line, const char *name, const char *label) {
for (int i = sc_lower_bound(w->defs, w->ndefs, line, &w->steps);
i < w->ndefs && w->defs[i].key == line; i++) {
w->steps++;
const CBMDefinition *d = &w->ctx->result->defs.items[w->defs[i].index];
if (d->label && strcmp(d->label, label) == 0 && d->name && strcmp(d->name, name) == 0) {
return w->defs[i].index;
}
}
return -1;
}
/* ── Events ────────────────────────────────────────────────────────── */
static void sc_emit(sc_walk_t *w, uint8_t kind, const char *name, const char *qual, int32_t index,
uint32_t pos, uint8_t flags) {
if (w->ncond > 0) {
flags |= (uint8_t)CBM_SCOPE_CONDITIONAL;
}
CBMScopeEvent ev = {
.name = name, .qual = qual, .pos = pos, .index = index, .kind = kind, .flags = flags};
cbm_scope_events_push(&w->ctx->result->scope, w->ctx->arena, ev);
}
/* The child naming a definition: its first named child of a name kind. */
static TSNode sc_name_node(const sc_walk_t *w, TSNode n) {
TSNode none = {{0}, NULL, NULL};
for (const char *const *k = w->lang->name_kinds; k && *k; k++) {
uint32_t c = ts_node_named_child_count(n);
for (uint32_t i = 0; i < c; i++) {
TSNode ch = ts_node_named_child(n, i);
if (strcmp(ts_node_type(ch), *k) == 0) {
return ch;
}
}
}
return none;
}
static const char *sc_text(const sc_walk_t *w, TSNode n) {
return ts_node_is_null(n) ? NULL : cbm_node_text(w->ctx->arena, n, w->ctx->source);
}
/* A definition's full name: package, enclosing nesting types and the name
* ("a.b.Outer.Inner"); NULL for a row without packages and nesting, or inside
* a nesting type whose own full name is unknown. */
static const char *sc_full_name(sc_walk_t *w, const char *name) {
if (!w->lang->package_kind && !w->lang->nest_kind) {
return NULL;
}
const char *outer = w->nnest > 0 ? w->nest_full[w->nnest - SKIP_ONE] : w->pkg;
if (!outer) {
return NULL;
}
return outer[0] ? cbm_arena_sprintf(w->ctx->arena, "%s.%s", outer, name)
: cbm_arena_strdup(w->ctx->arena, name);
}
/* Grow a pair of parallel arrays (pointer-sized and uint32 entries). */
static bool sc_grow(CBMArena *a, void **ptrs, size_t ptr_size, uint32_t **depths, uint32_t n,
uint32_t *cap) {
if (n < *cap) {
return true;
}
uint32_t ncap = *cap ? *cap * PAIR_LEN : CBM_SZ_16;
void *np = ptrs ? cbm_arena_alloc(a, ncap * ptr_size) : NULL;
uint32_t *nd = cbm_arena_alloc(a, ncap * sizeof(*nd));
if ((ptrs && !np) || !nd) {
return false;
}
if (n) {
if (ptrs) {
memcpy(np, *ptrs, n * ptr_size);
}
memcpy(nd, *depths, n * sizeof(*nd));
}
if (ptrs) {
*ptrs = np;
}
*depths = nd;
*cap = ncap;
return true;
}
/* A nesting type's full name (NULL: unknown) for the types nested in it. */
static bool sc_push_nest(sc_walk_t *w, const char *full) {
if (!sc_grow(w->ctx->arena, (void **)&w->nest_full, sizeof(*w->nest_full), &w->nest_depth,
w->nnest, &w->cap_nest)) {
return false;
}
w->nest_full[w->nnest] = full;
w->nest_depth[w->nnest] = w->depth;
w->nnest++;
return true;
}
static bool sc_type(sc_walk_t *w, TSNode n, const sc_rule_t *rule) {
const char *name = sc_text(w, sc_name_node(w, n));
const char *full = name ? sc_full_name(w, name) : NULL;
if (w->lang->nest_kind && strcmp(ts_node_type(n), w->lang->nest_kind) == 0 &&
!sc_push_nest(w, full)) {
return false; /* what is nested in it is named from here */
}
if (!name) {
return true;
}
uint32_t line = ts_node_start_point(n).row + TS_LINE_OFFSET;
int d = sc_find_def(w, line, name, rule->label);
sc_emit(w, d >= 0 ? (uint8_t)CBM_SCOPE_DEF : (uint8_t)CBM_SCOPE_ASSIGN, name,
d >= 0 ? full : NULL, d, ts_node_end_byte(n), 0);
return true;
}
/* An include statement: one INCLUDE event per import row it made. */
static void sc_include(sc_walk_t *w, TSNode n) {
uint32_t at = ts_node_start_byte(n);
uint8_t flags = 0;
if (w->lang->public_reexports) {
/* `import public "x.proto";`: the grammar's own `public` token (a
* comment holding the word is no keyword) */
uint32_t nc = ts_node_child_count(n);
for (uint32_t i = 0; i < nc; i++) {
TSNode ch = ts_node_child(n, i);
if (!ts_node_is_named(ch) && strcmp(ts_node_type(ch), "public") == 0) {
flags |= CBM_SCOPE_REEXPORT;
break;
}
}
}
/* the importing file's package, which its references are relative to */
const char *pkg = w->lang->package_kind ? w->pkg : NULL;
for (int k = sc_lower_bound(w->imps, w->nimps, at, &w->steps);
k < w->nimps && w->imps[k].key == at; k++) {
w->steps++;
sc_emit(w, CBM_SCOPE_INCLUDE, NULL, pkg, w->imps[k].index, ts_node_end_byte(n), flags);
}
}
/* A qualified type reference as written, blanks and comments between its
* parts dropped: "google.protobuf.Empty", ".a.B", "Empty". */
static void sc_qref(sc_walk_t *w, TSNode n) {
uint32_t b = ts_node_start_byte(n);
uint32_t e = ts_node_end_byte(n);
char *text = cbm_arena_alloc(w->ctx->arena, (size_t)(e - b) + SKIP_ONE);
if (!text) {
return;
}
size_t k = 0;
uint32_t c = ts_node_child_count(n);
for (uint32_t i = 0; i < c; i++) {
TSNode ch = ts_node_child(n, i);
if (strcmp(ts_node_type(ch), "comment") == 0) {
continue;
}
uint32_t cb = ts_node_start_byte(ch);
uint32_t ce = ts_node_end_byte(ch);
memcpy(text + k, w->ctx->source + cb, ce - cb);
k += ce - cb;
}
text[k] = '\0';
if (k > 0) {
sc_emit(w, CBM_SCOPE_QREF, text, NULL, (int32_t)b, e, 0);
}
}
/* Whether the walk descends into n; false also when memory ran out (the
* caller stops the walk). */
static bool sc_visit(sc_walk_t *w, TSNode n, bool *failed) {
if (!ts_node_is_named(n)) {
return true; /* a keyword token (`import`, `message`) is no statement of its kind */
}
const char *k = ts_node_type(n);
for (const sc_rule_t *r = w->lang->rules; r->kind; r++) {
if (strcmp(r->kind, k) != 0) {
continue;
}
switch (r->role) {
case SC_ROLE_TYPE:
*failed = !sc_type(w, n, r);
return true;
case SC_ROLE_INCLUDE:
sc_include(w, n);
return true;
case SC_ROLE_QREF:
sc_qref(w, n);
return false;
case SC_ROLE_NONE:
default:
return true;
}
}
return true;
}
/* Source order; one statement's events by kind, index, then name: a total
* order, so the array never depends on the sort's stability. */
static int sc_event_cmp(const void *a, const void *b) {
const CBMScopeEvent *x = a;
const CBMScopeEvent *y = b;
if (x->pos != y->pos) {
return x->pos < y->pos ? -1 : 1;
}
if (x->kind != y->kind) {
return x->kind < y->kind ? -1 : 1;
}
if (x->index != y->index) {
return x->index < y->index ? -1 : 1;
}
return strcmp(x->name ? x->name : "", y->name ? y->name : "");
}
/* Leaving every node at `depth` or deeper: their parse errors and nesting
* types no longer enclose the cursor. */
static void sc_leave(sc_walk_t *w, uint32_t depth) {
while (w->ncond > 0 && w->cond_depth[w->ncond - SKIP_ONE] >= depth) {
w->ncond--;
}
while (w->nnest > 0 && w->nest_depth[w->nnest - SKIP_ONE] >= depth) {
w->nnest--;
}
}
/* Depth-first over the tree with a cursor (no recursion). Returns the nodes
* visited; stops early only when memory runs out. */
static uint64_t sc_walk_tree(sc_walk_t *w, TSNode root) {
uint64_t visited = 0;
TSTreeCursor cur = ts_tree_cursor_new(root);
uint32_t depth = 0;
for (;;) {
TSNode n = ts_tree_cursor_current_node(&cur);
visited++;
sc_leave(w, depth);
w->depth = depth;
if (strcmp(ts_node_type(n), "ERROR") == 0) {
/* a parse the facts cannot trust: what follows is not proven, and
* what it holds may or may not be meant */
if (!sc_grow(w->ctx->arena, NULL, 0, &w->cond_depth, w->ncond, &w->cap_cond)) {
break;
}
w->cond_depth[w->ncond++] = depth;
sc_emit(w, CBM_SCOPE_OPAQUE, NULL, NULL, -1, ts_node_end_byte(n), 0);
}
bool failed = false;
bool descend = sc_visit(w, n, &failed);
if (failed) {
break;
}
if (descend && ts_tree_cursor_goto_first_child(&cur)) {
depth++;
continue;
}
bool moved = false;
while (!moved) {
if (ts_tree_cursor_goto_next_sibling(&cur)) {
moved = true;
} else if (depth == 0 || !ts_tree_cursor_goto_parent(&cur)) {
break;
} else {
depth--;
}
}
if (!moved) {
break;
}
}
ts_tree_cursor_delete(&cur);
return visited;
}
/* The file's package: the first named child of its package statement. */
static const char *sc_package(sc_walk_t *w, TSNode root) {
uint32_t c = ts_node_named_child_count(root);
for (uint32_t i = 0; i < c; i++) {
TSNode ch = ts_node_named_child(root, i);
if (strcmp(ts_node_type(ch), w->lang->package_kind) == 0 &&
ts_node_named_child_count(ch) > 0) {
const char *pk = sc_text(w, ts_node_named_child(ch, 0));
return pk ? pk : "";
}
}
return "";
}
void cbm_extract_scope(CBMExtractCtx *ctx) {
const sc_lang_t *lang = ctx ? sc_lang(ctx->language) : NULL;
if (!lang || ts_node_is_null(ctx->root)) {
return;
}
sc_walk_t w = {.ctx = ctx, .lang = lang, .pkg = ""};
if (lang->package_kind) {
w.pkg = sc_package(&w, ctx->root);
}
const CBMDefArray *defs = &ctx->result->defs;
if (defs->count > 0) {
w.defs = cbm_arena_alloc(ctx->arena, (size_t)defs->count * sizeof(*w.defs));
if (!w.defs) {
return;
}
for (int i = 0; i < defs->count; i++) {
w.defs[w.ndefs++] = (sc_ref_t){defs->items[i].start_line, i};
}
qsort(w.defs, (size_t)w.ndefs, sizeof(*w.defs), sc_ref_cmp);
}
const CBMImportArray *imps = &ctx->result->imports;
if (imps->count > 0) {
w.imps = cbm_arena_alloc(ctx->arena, (size_t)imps->count * sizeof(*w.imps));
if (!w.imps) {
return;
}
for (int i = 0; i < imps->count; i++) {
if (imps->items[i].module_path) {
w.imps[w.nimps++] = (sc_ref_t){imps->items[i].start_byte, i};
}
}
qsort(w.imps, (size_t)w.nimps, sizeof(*w.imps), sc_ref_cmp);
}
uint64_t visited = sc_walk_tree(&w, ctx->root);
atomic_fetch_add_explicit(&g_scope_nodes_visited, visited + w.steps, memory_order_relaxed);
CBMScopeEventArray *ev = &ctx->result->scope;
if (ev->count > SKIP_ONE) {
qsort(ev->items, (size_t)ev->count, sizeof(*ev->items), sc_event_cmp);
}
}
+382 -24
View File
@@ -5,13 +5,267 @@
#include "preprocessor.h"
#include "vendored/simplecpp/simplecpp.h"
#include <deque>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <cstdint>
// ── #include lookup against the indexed repository root (#2993) ────────────
// simplecpp looks a quoted header up as `dirPath(includer) + name` and opens it
// relative to the process working directory, so the expansion depended on the
// directory the indexer was started from. Here every #include and
// __has_include of the file, and of each header it reaches, is resolved before
// simplecpp runs: a quoted name in the including file's directory within the
// repository root, read through the caller's CBMIncludeReader; an angle-bracket
// name resolves to nothing, as no include directories are configured. The
// result is stored in simplecpp's FileDataCache under the exact key its own
// lookup (FileDataCache::get) computes, so simplecpp opens no file itself and
// the expansion is a function of the indexed tree alone.
namespace {
// A computed `#include MACRO` line names its header only after macro expansion,
// which no lookup made ahead of simplecpp can see. Its directive is renamed to
// one simplecpp does not know, and an unknown directive includes nothing.
const char kUnresolvedIncludeDirective[] = "cbm_unresolved_include";
// True when `key`, the normalised name an include resolves to, names a file
// within the indexed repository root: the name was written relative to the
// including file, and every component of the normalised result names an entry
// of the tree. The rule is the same on every platform, so one tree resolves the
// same way everywhere.
bool names_tree_file(const std::string &header, const std::string &key) {
if (header.empty() || header[0] == '/' || header[0] == '\\' ||
(header.size() >= 2U && header[1] == ':')) {
return false;
}
if (key.empty() || key[0] == '/' || key.find(':') != std::string::npos ||
key.find('\\') != std::string::npos || key.find('\0') != std::string::npos) {
return false;
}
std::string::size_type start = 0;
for (;;) {
const std::string::size_type end = key.find('/', start);
const std::string part =
key.substr(start, end == std::string::npos ? std::string::npos : end - start);
if (part == "..") {
return false;
}
if (end == std::string::npos) {
return true;
}
start = end + 1U;
}
}
// The first non-comment token after `tok` on the same line, or nullptr.
simplecpp::Token *next_on_line(simplecpp::Token *tok) {
simplecpp::Token *next = tok->next;
while (next && next->comment) {
next = next->next;
}
return sameline(tok, next) ? next : nullptr;
}
class IncludeTree {
public:
IncludeTree(const CBMIncludeReader *reader, std::vector<std::string> &files,
simplecpp::FileDataCache &cache)
: reader_(reader), files_(files), cache_(cache) {}
// Resolve everything the main file reaches. Every file name its own
// tokens registered (itself, and names from its #line directives) is the
// main file's; every later one belongs to a header.
void add_main_file(simplecpp::TokenList &tokens) {
first_header_file_ = static_cast<unsigned int>(files_.size());
prepare(tokens);
while (!pending_.empty()) {
Pending next = std::move(pending_.front());
pending_.pop_front();
prepare(next.tokens);
cache_.insert(simplecpp::FileData{next.key, std::move(next.tokens)});
}
}
// Tokens whose file index is at or above this came from a header.
unsigned int first_header_file() const {
return first_header_file_;
}
// The repository files the lookup read, sorted (the map's order).
void read_files(std::vector<std::string> *out) const {
for (std::map<std::string, bool>::const_iterator it = loaded_.begin(); it != loaded_.end();
++it) {
if (it->second) {
out->push_back(it->first);
}
}
}
private:
struct Pending {
std::string key;
simplecpp::TokenList tokens;
};
// Rewrite one file's __has_include tests and computed includes, and
// resolve the headers its #include lines name.
void prepare(simplecpp::TokenList &tokens) {
for (simplecpp::Token *tok = tokens.front(); tok; tok = tok->next) {
if (tok->str() == HAS_INCLUDE) {
rewrite_has_include(tokens, tok);
} else if (tok->op == '#' && !sameline(tok->previousSkipComments(), tok)) {
simplecpp::Token *directive = next_on_line(tok);
if (directive && directive->str() == INCLUDE) {
resolve_include(directive);
}
}
}
}
// `#include "name"` / `#include <name>` as simplecpp's preprocess reads it:
// the first non-comment token after the directive, quotes or brackets
// stripped.
void resolve_include(simplecpp::Token *directive) {
const simplecpp::Token *operand = next_on_line(directive);
if (!operand) {
return; // no header name: simplecpp reports it and opens nothing
}
if (operand->name) {
directive->setstr(kUnresolvedIncludeDirective);
return;
}
const std::string &spelled = operand->str();
if (spelled.size() <= 2U) {
return; // no header name: simplecpp reports it and opens nothing
}
(void)lookup(file_of(directive), spelled.substr(1U, spelled.size() - 2U),
spelled[0] == '<');
}
// `__has_include ( name )` (or without the parentheses) as simplecpp's #if
// evaluation reads it becomes the literal 1 or 0 of the same lookup. The
// operand of `defined` / `#ifdef` / `#ifndef` stays as it is. A form that
// does not close on its line keeps its operand, and its #if still fails to
// evaluate, as it did before.
void rewrite_has_include(simplecpp::TokenList &tokens, simplecpp::Token *tok) {
const simplecpp::Token *before = tok->previousSkipComments();
if (before && before->op == '(' && sameline(before, tok)) {
before = before->previousSkipComments();
}
if (before && sameline(before, tok) &&
(before->str() == DEFINED || before->str() == IFDEF || before->str() == IFNDEF)) {
return;
}
simplecpp::Token *operand = next_on_line(tok);
const bool par = operand && operand->op == '(';
if (par) {
operand = next_on_line(operand);
}
bool found = false;
simplecpp::Token *last = operand;
if (operand && operand->op == '<') {
while (last && last->op != '>') {
last = next_on_line(last);
}
} else if (operand && operand->str().size() > 2U && operand->str()[0] == '"') {
const std::string &spelled = operand->str();
found = lookup(file_of(tok), spelled.substr(1U, spelled.size() - 2U), false);
}
if (last && par) {
last = next_on_line(last);
if (last && last->op != ')') {
last = nullptr;
}
}
tok->setstr(found ? "1" : "0");
if (!last) {
return;
}
while (tok->next != last) {
tokens.deleteToken(tok->next);
}
tokens.deleteToken(last);
}
const std::string &file_of(const simplecpp::Token *tok) const {
static const std::string none;
return tok->location.fileIndex < files_.size() ? files_[tok->location.fileIndex] : none;
}
// Key the name the way simplecpp's FileDataCache::get does and make sure
// the cache holds that key, so get() finds it instead of opening a file.
// True when the name resolved to a file of the indexed tree.
bool lookup(const std::string &sourcefile, const std::string &header, bool system) {
if (simplecpp::isAbsolutePath(header)) {
return load(simplecpp::simplifyPath(header), false);
}
if (system) {
return false; // no include directories: get() opens nothing
}
const std::string key = simplecpp::simplifyPath(dirPath(sourcefile) + header);
return load(key, names_tree_file(header, key));
}
// Cache `key` once: its tokens when the tree holds it, an empty token list
// otherwise (the include then adds nothing). The main file never resolves
// as a header: its tokens are the expansion itself.
bool load(const std::string &key, bool in_tree) {
const std::map<std::string, bool>::const_iterator seen = loaded_.find(key);
if (seen != loaded_.end()) {
return seen->second;
}
std::string text;
const bool found = in_tree && !names_main_file(key) && read(key, &text);
loaded_[key] = found;
if (!found) {
cache_.insert(simplecpp::FileData{key, simplecpp::TokenList(files_)});
return false;
}
std::istringstream istr(text);
pending_.push_back(Pending{key, simplecpp::TokenList(istr, files_, key)});
return true;
}
bool names_main_file(const std::string &key) const {
for (unsigned int i = 0; i < first_header_file_ && i < files_.size(); i++) {
if (files_[i] == key) {
return true;
}
}
return false;
}
bool read(const std::string &key, std::string *out) const {
if (!reader_ || !reader_->read) {
return false;
}
size_t len = 0;
char *data = reader_->read(reader_->userdata, key.c_str(), &len);
if (!data) {
return false;
}
out->assign(data, len);
if (reader_->release) {
reader_->release(reader_->userdata, data);
}
return true;
}
const CBMIncludeReader *reader_;
std::vector<std::string> &files_;
simplecpp::FileDataCache &cache_;
std::map<std::string, bool> loaded_;
std::deque<Pending> pending_;
unsigned int first_header_file_ = 0;
};
} // namespace
extern "C" {
static bool has_preprocessor_work(const char *source, int source_len) {
@@ -407,11 +661,23 @@ static bool tokens_source_adjacent(const simplecpp::Token *prev, const simplecpp
// scopes, C-LSP class scopes) minted a second node for the same definition, the
// QN dedup (strcmp) missing it. A gap that held whitespace or a comment is still
// one space, as before.
static std::string stringify_source_spacing(const simplecpp::TokenList &tokens) {
//
// Tokens of an included header's body (file index at or above
// first_header_file) are left out: the header is indexed as its own file, and
// its calls, usages and writes are its own, never the includer's. What the
// header defines still shapes the main file's tokens, since macro expansions
// carry the location of their use. With no header body in the buffer, the main
// file's lines keep their own numbers, as they do when nothing is included.
static std::string stringify_source_spacing(const simplecpp::TokenList &tokens,
unsigned int first_header_file) {
std::ostringstream ret;
simplecpp::Location loc;
loc.line = 1;
const simplecpp::Token *prev = nullptr;
for (const simplecpp::Token *tok = tokens.cfront(); tok; tok = tok->next) {
if (tok->location.fileIndex >= first_header_file) {
continue;
}
if (tok->location.line < loc.line || tok->location.fileIndex != loc.fileIndex) {
ret << "\n#line " << tok->location.line << " \"" << tokens.file(tok->location)
<< "\"\n";
@@ -421,16 +687,113 @@ static std::string stringify_source_spacing(const simplecpp::TokenList &tokens)
ret << '\n';
loc.line++;
}
const simplecpp::Token *prev = tok->previous;
if (prev && prev->location.sameline(tok->location) && !tokens_source_adjacent(prev, tok)) {
ret << ' ';
}
ret << tok->str();
loc.adjust(tok->str());
prev = tok;
}
return ret.str();
}
// simplecpp drops the whole expansion when it stops at #error, or at a
// directive it cannot read or evaluate: the output list is cleared and an
// ERROR or SYNTAX_ERROR is reported.
static bool expansion_discarded(const simplecpp::TokenList &output,
const simplecpp::OutputList &outputs) {
if (!output.empty()) {
return false;
}
for (simplecpp::OutputList::const_iterator it = outputs.begin(); it != outputs.end(); ++it) {
if (it->type == simplecpp::Output::ERROR || it->type == simplecpp::Output::SYNTAX_ERROR) {
return true;
}
}
return false;
}
// One simplecpp run over the file. Every header its #include and
// __has_include lines reach is resolved through `reader` first (none when it
// is NULL); `read` (may be NULL) receives the repository files the lookup
// read, also when the run fails. Returns false when simplecpp discarded the
// expansion.
static bool preprocess_file(const std::string &src, const std::string &main_name,
const simplecpp::DUI &dui, const CBMIncludeReader *reader,
std::string *expanded, std::vector<std::string> *read) {
std::vector<std::string> files;
files.push_back(main_name);
std::istringstream istr(src);
simplecpp::TokenList rawtokens(istr, files, files[0]);
simplecpp::FileDataCache cache;
IncludeTree tree(reader, files, cache);
bool kept = false;
try {
tree.add_main_file(rawtokens);
simplecpp::TokenList output(files);
simplecpp::OutputList outputs;
simplecpp::preprocess(output, rawtokens, files, cache, dui, &outputs);
kept = !expansion_discarded(output, outputs);
*expanded = stringify_source_spacing(output, tree.first_header_file());
} catch (...) {
if (read) {
tree.read_files(read);
}
throw;
}
if (read) {
tree.read_files(read);
}
simplecpp::cleanup(cache);
return kept;
}
// The file's expansion with its includes followed. When what an include
// brought in stops simplecpp (an #error reached inside a header, a header
// directive it cannot evaluate, a failure while reading one), the file keeps
// its expansion without includes instead of losing it. `read` receives the
// repository files the include-following run read.
static std::string preprocess_with_fallback(const std::string &src, const std::string &main_name,
const simplecpp::DUI &dui,
const CBMIncludeReader *reader,
std::vector<std::string> *read) {
std::string expanded;
if (reader) {
try {
if (preprocess_file(src, main_name, dui, reader, &expanded, read)) {
return expanded;
}
} catch (...) {
// fall through to the expansion without includes
}
expanded.clear();
}
(void)preprocess_file(src, main_name, dui, nullptr, &expanded, nullptr);
return expanded;
}
// A malloc'd copy of `read` for CBMPreprocessedSource.included_files; false on
// allocation failure (everything copied so far is released by the caller).
static bool copy_included_files(const std::vector<std::string> &read, CBMPreprocessedSource *pp) {
if (read.empty()) {
return true;
}
pp->included_files = (char **)calloc(read.size(), sizeof(char *));
if (!pp->included_files) {
return false;
}
for (size_t i = 0; i < read.size(); i++) {
char *copy = (char *)malloc(read[i].size() + 1);
if (!copy) {
return false;
}
memcpy(copy, read[i].c_str(), read[i].size() + 1);
pp->included_files[i] = copy;
pp->included_count = (int)i + 1;
}
return true;
}
static bool build_line_map(const std::string &expanded, const std::string &main_file,
uint32_t *original_line_by_expanded_line,
uint8_t *belongs_to_main_file) {
@@ -472,7 +835,9 @@ static bool build_line_map(const std::string &expanded, const std::string &main_
CBMPreprocessedSource *cbm_preprocess_with_map(const char *source, int source_len,
const char *filename, const char **extra_defines,
const char **include_paths, int cpp_mode) {
const char **include_paths, int cpp_mode,
const CBMIncludeReader *reader) {
(void)include_paths; // lookups resolve through `reader` only (preprocessor.h)
// Run the second pass when there are directives to evaluate OR when the file
// carries export-macro-shaped identifiers to predefine empty (#1989) — a UE
// plugin header with only `#pragma once` + `#include` lines still needs it.
@@ -509,32 +874,21 @@ CBMPreprocessedSource *cbm_preprocess_with_map(const char *source, int source_le
dui.defines.push_back(std::string(export_cands[i]) + "=");
}
}
if (include_paths) {
for (int i = 0; include_paths[i]; i++)
dui.includePaths.push_back(include_paths[i]);
}
dui.std = cpp_mode ? "c++17" : "c11";
std::string src(source, source_len);
std::istringstream istr(src);
std::vector<std::string> files;
files.push_back(filename ? filename : "<input>");
simplecpp::TokenList rawtokens(istr, files, files[0]);
simplecpp::TokenList output(files);
simplecpp::FileDataCache filedata = simplecpp::load(rawtokens, files, dui);
simplecpp::preprocess(output, rawtokens, files, filedata, dui);
std::string result = stringify_source_spacing(output);
// Clean up loaded file data
simplecpp::cleanup(filedata);
const std::string src(source, source_len);
const std::string main_name(filename ? filename : "<input>");
std::vector<std::string> read;
std::string result = preprocess_with_fallback(src, main_name, dui, reader, &read);
CBMPreprocessedSource *pp = (CBMPreprocessedSource *)calloc(1, sizeof(*pp));
if (!pp) {
return NULL;
}
if (!copy_included_files(read, pp)) {
cbm_preprocessed_source_free(pp);
return NULL;
}
int line_count = count_expanded_lines(result);
pp->source = (char *)malloc(result.size() + 1);
pp->original_line_by_expanded_line =
@@ -546,7 +900,7 @@ CBMPreprocessedSource *cbm_preprocess_with_map(const char *source, int source_le
return NULL;
}
memcpy(pp->source, result.c_str(), result.size() + 1);
if (!build_line_map(result, files[0], pp->original_line_by_expanded_line,
if (!build_line_map(result, main_name, pp->original_line_by_expanded_line,
pp->belongs_to_main_file)) {
cbm_preprocessed_source_free(pp);
return NULL;
@@ -561,7 +915,7 @@ CBMPreprocessedSource *cbm_preprocess_with_map(const char *source, int source_le
char *cbm_preprocess(const char *source, int source_len, const char *filename,
const char **extra_defines, const char **include_paths, int cpp_mode) {
CBMPreprocessedSource *pp = cbm_preprocess_with_map(source, source_len, filename, extra_defines,
include_paths, cpp_mode);
include_paths, cpp_mode, NULL);
if (!pp) {
return NULL;
}
@@ -582,6 +936,10 @@ void cbm_preprocessed_source_free(CBMPreprocessedSource *pp) {
free(pp->source);
free(pp->original_line_by_expanded_line);
free(pp->belongs_to_main_file);
for (int i = 0; i < pp->included_count; i++) {
free(pp->included_files[i]);
}
free(pp->included_files);
free(pp);
}
+30 -3
View File
@@ -22,28 +22,55 @@ typedef struct {
uint32_t *original_line_by_expanded_line; // 1-based; 0 means directive/unmapped.
uint8_t *belongs_to_main_file; // 1-based; true only for the original input file.
int expanded_line_count;
/* The repository files the #include lookup read (repository-relative,
* sorted, each once), whether or not the run that followed includes kept
* its expansion: the expansion depends on their bytes either way. */
char **included_files;
int included_count;
} CBMPreprocessedSource;
/* Supplies the files of the indexed repository to the #include lookup of the
* preprocessed pass. rel_path is relative to the repository root, '/'-separated
* and normalised. read returns a buffer of *out_len bytes, handed back to
* release once consumed, or NULL when the repository holds no such file. */
typedef struct {
char *(*read)(const void *userdata, const char *rel_path, size_t *out_len);
void (*release)(const void *userdata, char *data);
const void *userdata;
} CBMIncludeReader;
// Collect conventional export-macro-shaped identifiers (ALL_CAPS ending in
// _API/_EXPORT/_IMPORT/_DLLEXPORT/_DEPRECATED) from the source, up to max_out
// unique names. Returns the number of names stored in out.
int cbm_export_macro_candidates(const char *source, int source_len,
char (*out)[CBM_EXPORT_MACRO_NAME_MAX], int max_out);
// Preprocess C/C++ source: expand macros, evaluate #ifdef, resolve #include.
// Preprocess C/C++ source: expand macros, evaluate #ifdef. #include is not
// followed (no include reader): every included header contributes nothing.
// Returns malloc-allocated expanded source, or NULL if no expansion needed/on failure.
// extra_defines: NULL-terminated array of "NAME=VALUE" strings (can be NULL).
// include_paths: NULL-terminated array of directory paths for #include resolution (can be NULL).
// include_paths: kept for API compatibility; not consulted (see cbm_preprocess_with_map).
// The returned string must be freed with cbm_preprocess_free().
char *cbm_preprocess(const char *source, int source_len, const char *filename,
const char **extra_defines, const char **include_paths, int cpp_mode);
// Preprocess and return source plus expanded-line -> original-line ownership map.
// filename is the file's path relative to the indexed repository root. Quoted
// #include and __has_include names resolve through `reader` only, in the
// including file's directory within the repository root; a NULL reader
// resolves nothing. Angle-bracket names and computed #include lines resolve to
// nothing, and include_paths is not consulted, so the expansion is a function
// of the indexed tree alone. The expanded buffer holds the main file's own
// tokens only: what an included header defines (macros, #if state) shapes it,
// but the header's body is not part of it; the header is indexed as its own
// file. When the run that follows includes ends in #error (or another stop that
// discards the expansion), the file falls back to its expansion without includes.
// Returns NULL if no expansion is needed or preprocessing fails.
// Free with cbm_preprocessed_source_free().
CBMPreprocessedSource *cbm_preprocess_with_map(const char *source, int source_len,
const char *filename, const char **extra_defines,
const char **include_paths, int cpp_mode);
const char **include_paths, int cpp_mode,
const CBMIncludeReader *reader);
// Free preprocessed source returned by cbm_preprocess.
void cbm_preprocess_free(char *expanded);
+6
View File
@@ -386,6 +386,11 @@ static void cr_walk(cr_ctx_t *c, CBMFileResult *r) {
cr_str(c, &r->imports.items[i].module_path);
cr_str(c, &r->imports.items[i].member);
}
cr_array(c, (void **)&r->scope.items, r->scope.count, sizeof(CBMScopeEvent));
for (int i = 0; i < r->scope.count && r->scope.items; i++) {
cr_str(c, &r->scope.items[i].name);
cr_str(c, &r->scope.items[i].qual);
}
cr_array(c, (void **)&r->usages.items, r->usages.count, sizeof(CBMUsage));
for (int i = 0; i < r->usages.count && r->usages.items; i++) {
cr_str(c, &r->usages.items[i].ref_name);
@@ -471,6 +476,7 @@ static void cr_walk(cr_ctx_t *c, CBMFileResult *r) {
cr_list(c, &r->constants);
cr_list(c, &r->global_vars);
cr_list(c, &r->macros);
cr_list(c, &r->included_files);
cr_str(c, &r->error_msg);
cr_str(c, &r->error_ranges);
cr_str(c, &r->module_doc);
+1 -1
View File
@@ -55,7 +55,7 @@ src/mcp/mcp.c 765
src/pipeline/artifact.c 38
src/pipeline/fqn.c 23
src/pipeline/lsp_resolve.h 4
src/pipeline/lsp_surface.c 5
src/pipeline/lsp_surface.c 4
src/pipeline/pass_calls.c 22
src/pipeline/pass_compile_commands.c 17
src/pipeline/pass_configlink.c 3
+38
View File
@@ -360,6 +360,27 @@ database:
host: localhost
YAMLEOF
# An Ansible playbook naming a role, under deploy/ (walked for its
# configuration files). The playbook extracts no imports of its own: its role
# edge comes from the Ansible pass, so the semantic pass builds this file's
# import map from those edges (its slow path), on the sequential route a
# project this small always takes. Those keys must come from the memory core
# like the fast path's: libc copies released through the core crashed the
# index worker of the PRODUCTION build (mimalloc core, macOS), which the unit
# runner (libc core) cannot see. Phase 2 must report "indexed"; Phase 3 checks
# that the role import was drawn, so the slow path was reached.
mkdir -p "$TMPDIR/deploy/roles/web/tasks"
cat > "$TMPDIR/deploy/site.yml" << 'YAMLEOF'
- hosts: web
roles:
- web
YAMLEOF
cat > "$TMPDIR/deploy/roles/web/tasks/main.yml" << 'YAMLEOF'
- name: Install
apt:
name: nginx
YAMLEOF
# C++ crash reproduction (#424): a large, templated C++ header. The vendored
# tree-sitter runtime previously corrupted the heap and SEGV'd mid-parse on
# large templated C++ in the PRODUCTION build (MI_OVERRIDE=1) — most reliably on
@@ -446,6 +467,23 @@ if [ "$TOTAL" -lt 1 ]; then
exit 1
fi
# 3a-ansible: the playbook under deploy/ (Phase 2) drew its role import, so
# the semantic pass built that file's import map on its slow path. Both ends
# are matched by name (the playbook's File node, the role's Folder node), like
# every other check here, so no path separator or prefix enters the match.
if ! ANSIBLE_IMPORTS=$(cli query_graph --project "$PROJECT" --query \
"MATCH (a:File)-[r:IMPORTS]->(b:Folder) WHERE a.name = 'site.yml' AND b.name = 'web' RETURN b.name"); then
echo "FAIL: query_graph for the Ansible role import exited non-zero"; cat "$CLI_STDERR"; exit 1
fi
ANSIBLE_ROWS=$(echo "$ANSIBLE_IMPORTS" | sed -n 's/^total: //p' | head -1)
ANSIBLE_ROWS=${ANSIBLE_ROWS:-0}
if [ "$ANSIBLE_ROWS" -lt 1 ]; then
echo "FAIL: the Ansible playbook under deploy/ has no role import"
echo "$ANSIBLE_IMPORTS"
exit 1
fi
echo "OK: Ansible playbook under deploy/ imports its role"
echo ""
echo "=== Phase 3z: structuredContent carries structure or is absent — never {} and never a copy (#1375, #1522) ==="
# Asserts on the SHIPPED artifact what the unit suite asserts from source: a
+175 -21
View File
@@ -48,7 +48,7 @@ static const char *ALWAYS_SKIP_DIRS[] = {
".idea", ".vs", ".vscode", ".eclipse", ".claude", ".claude-worktrees", "Antigravity",
/* Python */
".cache", ".eggs", ".env", ".mypy_cache", ".nox", ".pytest_cache", ".ruff_cache", ".tox",
".venv", "__pycache__", "env", "htmlcov", "site-packages", "venv",
".venv", "__pycache__", "htmlcov", "site-packages", "venv",
/* JS/TS */
".npm", ".nyc_output", ".pnpm-store", ".yarn", "bower_components", "coverage", "node_modules",
".next", ".nuxt", ".svelte-kit", ".angular", ".turbo", ".parcel-cache", ".docusaurus", ".expo",
@@ -59,10 +59,38 @@ static const char *ALWAYS_SKIP_DIRS[] = {
".cargo", ".stack-work", ".dart_tool", "zig-cache", "zig-out", ".metals", ".bloop", ".bsp",
".ccls-cache", ".clangd", "elm-stuff", "_opam", ".cpcache", ".shadow-cljs",
/* Deploy */
".vercel", ".netlify", "deploy", "deployed",
".vercel", ".netlify",
/* Misc */
".codebase-memory", ".qdrant_code_embeddings", ".tmp", "vendor", "vendored", NULL};
/* Directories walked for their deployment and configuration files only. Build
* output copied into deploy/ made duplicate classes of the real source
* (discover_deploy_excluded_full_mode), while the manifests, charts, Terraform,
* Dockerfiles and scripts that deploy a project live there too. A file in such a
* directory, at any depth, is kept by its language
* (cbm_language_is_deployment_config), never by its folder. */
static const char *CONFIG_ONLY_DIRS[] = {"deploy", "deployed", NULL};
/* A directory named env is skipped only when it is a Python environment:
* `python -m venv` and virtualenv (since 20) write pyvenv.cfg, older
* virtualenv an activate script in bin/ (POSIX) or Scripts/ (Windows), and
* conda conda-meta/history into every environment, the base one included.
* Any other env directory, a Go package or a folder of settings, is walked
* like any other. A marker is its exact stored name, never a lookup the file
* system may answer case-insensitively. One marker list for the walk (the
* directory listings) and the pinned inventory (its file list). */
static const char VENV_DIR_NAME[] = "env";
const char *const CBM_DISCOVER_VENV_MARKERS[] = {"pyvenv.cfg", "bin/activate", "Scripts/activate",
"conda-meta/history", NULL};
/* Package manifests: what the package map reads (pass_pkgmap.c), and what a
* config-only directory drops by name. */
static const char *PACKAGE_MANIFESTS[] = {"package.json", "go.mod", "Cargo.toml",
"pyproject.toml", "composer.json", "pubspec.yaml",
"pom.xml", "build.gradle", "build.gradle.kts",
"mix.exs", "Package.swift", NULL};
static const char PACKAGE_MANIFEST_SUFFIX[] = ".gemspec";
/* Always-skip directories matched by relative-PATH suffix, not by basename:
* generated build output whose last path component alone ("views") is far too
* common to skip everywhere. A rule matches the whole relative path or a
@@ -446,6 +474,22 @@ bool cbm_should_skip_dir(const char *dirname, cbm_index_mode_t mode) {
return false;
}
bool cbm_discover_config_only_dir(const char *dirname) {
return dirname && str_in_list(dirname, CONFIG_ONLY_DIRS);
}
bool cbm_discover_venv_candidate(const char *name) {
return name && strcmp(name, VENV_DIR_NAME) == 0;
}
bool cbm_discover_is_package_manifest(const char *basename) {
if (!basename) {
return false;
}
return str_in_list(basename, PACKAGE_MANIFESTS) ||
ends_with(basename, strlen(basename), PACKAGE_MANIFEST_SUFFIX);
}
bool cbm_has_ignored_suffix(const char *filename, cbm_index_mode_t mode) {
if (!filename) {
return false;
@@ -858,24 +902,35 @@ static bool dir_is_cache_tree(const char *abs_path) {
* which yield to tracked content; built-in skips and .cbmignore never do. */
typedef enum { DIR_KEEP, DIR_SKIP, DIR_GIT_IGNORED } dir_verdict_t;
static bool dir_is_venv_on_disk(const char *abs_dir);
/* *config_only: the directory is walked for its configuration files only
* (CONFIG_ONLY_DIRS); its subdirectories inherit that from the walk frame. */
static dir_verdict_t directory_verdict(const char *entry_name, const char *rel_path,
const cbm_discover_opts_t *opts,
const char *abs_path, const cbm_discover_opts_t *opts,
const gitignore_link_t *ignore_chain,
const cbm_gitignore_t *global_gi,
const cbm_gitignore_t *cbmignore) {
bool builtin_skip = cbm_should_skip_dir(entry_name, opts ? opts->mode : CBM_MODE_FULL) &&
(cbm_discover_is_safety_core_directory(entry_name) ||
!cbm_discover_path_in_jvm_package(rel_path));
if (builtin_skip || cbm_discover_path_has_skip_suffix(rel_path)) {
const cbm_gitignore_t *cbmignore, bool *config_only) {
*config_only = false;
bool safety = cbm_discover_is_safety_core_directory(entry_name);
bool named = cbm_should_skip_dir(entry_name, opts ? opts->mode : CBM_MODE_FULL) ||
(cbm_discover_venv_candidate(entry_name) && dir_is_venv_on_disk(abs_path));
bool config = !named && cbm_discover_config_only_dir(entry_name);
/* Below a JVM source root every directory is a package: no built-in rule. */
bool package = (named || config) && !safety && cbm_discover_path_in_jvm_package(rel_path);
bool builtin_skip = named && !package;
config = config && !package;
if (builtin_skip || config || cbm_discover_path_has_skip_suffix(rel_path)) {
/* #500: a .cbmignore negation (e.g. "!obj/") whose rule is the last
* match for this dir un-skips a built-in skip-list dir — except the
* non-negatable safety core. Fall through so .gitignore/global/local
* rules still apply to the un-skipped dir. */
bool unskipped = cbmignore && !cbm_discover_is_safety_core_directory(entry_name) &&
cbm_gitignore_match_result(cbmignore, rel_path, true) < 0;
if (!unskipped) {
* non-negatable safety core — and walks a config-only one in full.
* Fall through so .gitignore/global/local rules still apply. */
bool unskipped =
cbmignore && !safety && cbm_gitignore_match_result(cbmignore, rel_path, true) < 0;
if (!unskipped && !config) {
return DIR_SKIP;
}
*config_only = !unskipped;
}
bool git_ignored = gitignore_chain_result(ignore_chain, rel_path, true) > 0;
bool global_ignored = global_gi && cbm_gitignore_matches(global_gi, rel_path, true);
@@ -994,6 +1049,90 @@ static int safe_stat(const char *abs_path, struct stat *st, bool *is_symlink) {
#endif
}
/* True when the listing of `dir` holds an entry stored exactly as `name`
* (byte comparison, so letter case decides on every platform) that is a
* directory (`directory`) or a regular file, checked by safe_stat: no
* symlink or junction is followed out of the repository, no content read. */
static bool dir_lists_exact(const char *dir, const char *name, size_t name_len, bool directory) {
cbm_dir_t *d = cbm_opendir(dir);
if (!d) {
return false;
}
bool listed = false;
cbm_dirent_t *entry;
while (!listed && (entry = cbm_readdir(d)) != NULL) {
listed = strlen(entry->name) == name_len && memcmp(entry->name, name, name_len) == 0;
}
cbm_closedir(d);
char path[CBM_SZ_4K];
int written = snprintf(path, sizeof(path), "%s/%.*s", dir, (int)name_len, name);
if (!listed || written < 0 || (size_t)written >= sizeof(path)) {
return false;
}
struct stat st;
bool is_symlink = false;
return safe_stat(path, &st, &is_symlink) == 0 &&
(directory ? S_ISDIR(st.st_mode) : S_ISREG(st.st_mode));
}
/* One venv marker below abs_dir, one path component at a time: each one is
* an exact entry of the directory before it, a directory on the way, a
* regular file at the end. */
static bool venv_marker_listed(const char *abs_dir, const char *marker) {
char dir[CBM_SZ_4K];
int written = snprintf(dir, sizeof(dir), "%s", abs_dir);
if (written < 0 || (size_t)written >= sizeof(dir)) {
return false;
}
for (const char *part = marker; *part;) {
const char *slash = strchr(part, '/');
size_t len = slash ? (size_t)(slash - part) : strlen(part);
if (!dir_lists_exact(dir, part, len, slash != NULL)) {
return false;
}
if (!slash) {
return true;
}
size_t used = strlen(dir);
written = snprintf(dir + used, sizeof(dir) - used, "/%.*s", (int)len, part);
if (written < 0 || (size_t)written >= sizeof(dir) - used) {
return false;
}
part = slash + SKIP_ONE;
}
return false;
}
/* Once per directory named env the walk meets: its listing is read once to
* see which markers can be there, and a marker's subdirectory (bin, Scripts,
* conda-meta) only when it is listed. */
static bool dir_is_venv_on_disk(const char *abs_dir) {
/* the list's slots; the last one is its NULL terminator */
enum { SLOTS = sizeof(CBM_DISCOVER_VENV_MARKERS) / sizeof(CBM_DISCOVER_VENV_MARKERS[0]) };
bool first[SLOTS] = {false};
cbm_dir_t *d = abs_dir ? cbm_opendir(abs_dir) : NULL;
if (!d) {
return false;
}
cbm_dirent_t *entry;
while ((entry = cbm_readdir(d)) != NULL) {
for (int i = 0; i < SLOTS - SKIP_ONE; i++) {
const char *marker = CBM_DISCOVER_VENV_MARKERS[i];
const char *slash = strchr(marker, '/');
size_t len = slash ? (size_t)(slash - marker) : strlen(marker);
first[i] =
first[i] || (strlen(entry->name) == len && memcmp(entry->name, marker, len) == 0);
}
}
cbm_closedir(d);
for (int i = 0; i < SLOTS - SKIP_ONE; i++) {
if (first[i] && venv_marker_listed(abs_dir, CBM_DISCOVER_VENV_MARKERS[i])) {
return true;
}
}
return false;
}
/* ── Sphinx documentation sets ────────────────────────────────────
* A directory with a conf.py is a Sphinx documentation set, and its
* `source_suffix` says which file suffixes are reStructuredText (Django's
@@ -1146,7 +1285,8 @@ static void walk_dir_process_file(const char *abs_path, const char *rel_path, co
const gitignore_link_t *ignore_chain,
const cbm_gitignore_t *global_gi,
const cbm_gitignore_t *cbmignore, const sphinx_suffixes_t *sphinx,
bool in_ignored_dir, const struct stat *st, file_list_t *out) {
bool in_ignored_dir, bool config_only, const struct stat *st,
file_list_t *out) {
const char *skip_reason =
file_skip_reason(name, rel_path, opts, ignore_chain, global_gi, cbmignore, st->st_size,
tracked_has_file(out->tracked, rel_path), in_ignored_dir);
@@ -1165,6 +1305,13 @@ static void walk_dir_process_file(const char *abs_path, const char *rel_path, co
if (lang == CBM_LANG_COUNT) {
return;
}
if (config_only &&
(!cbm_language_is_deployment_config(lang) || cbm_discover_is_package_manifest(name))) {
/* Program source and package manifests in deploy/ or deployed/: the
* built-in skip list's rule for those directories (#963 records it). */
file_list_add_ignored(out, rel_path, "skip-list");
return;
}
fl_add(out, abs_path, rel_path, lang, st);
}
@@ -1174,6 +1321,7 @@ typedef struct {
const gitignore_link_t *ignore_chain; /* deepest .gitignore governing this dir */
sphinx_suffixes_t sphinx; /* the documentation set's extra reST suffixes */
bool git_ignored; /* ignored by git's rules, entered only for its tracked content */
bool config_only; /* in or below a CONFIG_ONLY_DIRS directory: configuration files only */
} walk_frame_t;
/* Initial capacity only — the stack grows on demand. A single directory can
* hold more pending sibling frames than any fixed cap (dotnet/runtime has 855
@@ -1211,7 +1359,8 @@ static cbm_gitignore_t *try_load_nested_gitignore(const walk_frame_t *frame) {
* context. Grows the stack geometrically; the caller's `parent` must not
* point into the stack array (walk_dir pops into a local copy). */
static void walk_push_subdir(walk_stack_t *ws, const char *abs_path, const char *rel_path,
const walk_frame_t *parent, bool git_ignored, file_list_t *out) {
const walk_frame_t *parent, bool git_ignored, bool config_only,
file_list_t *out) {
if (ws->top >= ws->cap) {
int new_cap = ws->cap * 2;
walk_frame_t *grown = realloc(ws->frames, (size_t)new_cap * sizeof(*grown));
@@ -1233,6 +1382,7 @@ static void walk_push_subdir(walk_stack_t *ws, const char *abs_path, const char
slot->ignore_chain = parent->ignore_chain;
slot->sphinx = parent->sphinx;
slot->git_ignored = git_ignored;
slot->config_only = config_only;
ws->top++;
}
@@ -1276,23 +1426,27 @@ static void walk_dir_process_entry(cbm_dirent_t *entry, const walk_frame_t *fram
}
if (S_ISDIR(st.st_mode)) {
dir_verdict_t verdict = dir_is_cache_tree(abs_path)
? DIR_SKIP
: directory_verdict(entry->name, rel_path, opts,
frame->ignore_chain, global_gi, cbmignore);
bool config_only = false;
dir_verdict_t verdict =
dir_is_cache_tree(abs_path)
? DIR_SKIP
: directory_verdict(entry->name, rel_path, abs_path, opts, frame->ignore_chain,
global_gi, cbmignore, &config_only);
if (verdict == DIR_KEEP && frame->git_ignored) {
verdict = DIR_GIT_IGNORED; /* below a git-ignored directory, as in git */
}
if (verdict == DIR_KEEP ||
(verdict == DIR_GIT_IGNORED && tracked_has_dir(out->tracked, rel_path))) {
walk_push_subdir(ws, abs_path, rel_path, frame, verdict == DIR_GIT_IGNORED, out);
walk_push_subdir(ws, abs_path, rel_path, frame, verdict == DIR_GIT_IGNORED,
frame->config_only || config_only, out);
} else {
/* Record the excluded subtree root so callers can report it (#411). */
file_list_add_excluded(out, rel_path);
}
} else if (S_ISREG(st.st_mode)) {
walk_dir_process_file(abs_path, rel_path, entry->name, opts, frame->ignore_chain, global_gi,
cbmignore, &frame->sphinx, frame->git_ignored, &st, out);
cbmignore, &frame->sphinx, frame->git_ignored, frame->config_only,
&st, out);
}
}
+27 -1
View File
@@ -162,9 +162,35 @@ typedef enum {
#endif
/* Check if a directory name should always be skipped (e.g. .git, node_modules).
* Only checks the basename, not the full path. */
* Only checks the basename, not the full path. Two built-in rules need more
* than the name and are not in this list: a directory named env is skipped only
* when it is a Python virtual environment, and deploy/ and deployed/ are walked
* for their configuration files only (cbm_discover_config_only_dir). */
bool cbm_should_skip_dir(const char *dirname, cbm_index_mode_t mode);
/* True for deploy and deployed: walked, but only files whose language passes
* cbm_language_is_deployment_config are indexed in them, at any depth; their
* program source is not. Not applied below a JVM source root, and lifted by a
* .cbmignore negation of the directory, as for the skip list. */
bool cbm_discover_config_only_dir(const char *dirname);
/* Build and configuration languages (Makefile, CMake, YAML, TOML, JSON, INI,
* Dockerfile): no cross-language call semantics. */
bool cbm_language_is_build_config(CBMLanguage lang);
/* The languages a config-only directory indexes: every build and
* configuration language, the other configuration and infrastructure
* languages (Properties, XML, JSON5, Jsonnet, Jinja2, Kustomize, Go Template,
* HCL, Bicep, Nix, Puppet, Pkl, DotEnv) and the shell family (Bash, Zsh, Fish,
* PowerShell). language.c. */
bool cbm_language_is_deployment_config(CBMLanguage lang);
/* A package manifest by its base name (package.json, go.mod, Cargo.toml,
* pyproject.toml, composer.json, pubspec.yaml, pom.xml, build.gradle[.kts],
* mix.exs, Package.swift, *.gemspec): what the package map reads. A
* config-only directory drops it whatever its language. */
bool cbm_discover_is_package_manifest(const char *basename);
/* Check if a file has a suffix that should be skipped (e.g. .pyc, .png). */
bool cbm_has_ignored_suffix(const char *filename, cbm_index_mode_t mode);
+6
View File
@@ -7,4 +7,10 @@ bool cbm_discover_path_has_skip_suffix(const char *relative_path);
bool cbm_discover_is_safety_core_directory(const char *name);
bool cbm_discover_path_in_jvm_package(const char *relative_path);
/* A directory of this name is skipped when one of CBM_DISCOVER_VENV_MARKERS
* (relative paths, NULL-terminated) is a regular file in it: the walk checks
* the disk without following links, the pinned inventory its file list. */
bool cbm_discover_venv_candidate(const char *name);
extern const char *const CBM_DISCOVER_VENV_MARKERS[];
#endif
+52 -9
View File
@@ -2,6 +2,8 @@
#include "discover/discover.h"
#include "discover/discover_policy_internal.h"
#include <string.h>
static bool cif_match(cif_context *c, const cbm_ignore_program_t *program,
cbm_inventory_path_t path, bool directory, cbm_ignore_decision_t *decision) {
*decision = CBM_IGNORE_NO_OPINION;
@@ -96,31 +98,68 @@ static bool cif_file_reason(cif_context *c, size_t parent, cbm_inventory_path_t
cif_ignore_reason(c, parent, path, false, reason);
}
/* The directory is a Python virtual environment: one of the shared markers is
* a file of the inventory (which holds regular files only, never a link). */
static bool cif_venv(cif_context *c, cbm_inventory_path_t path, bool *venv) {
*venv = false;
unsigned char buffer[CIF_PATH_MAX + 1];
for (size_t m = 0; CBM_DISCOVER_VENV_MARKERS[m] && !*venv; m++) {
size_t length = strlen(CBM_DISCOVER_VENV_MARKERS[m]);
/* A longer candidate cannot occur in this fully validated inventory. */
if (path.length > CIF_PATH_MAX - length - 1) {
continue;
}
if (!cif_copy(c, buffer, path.data, path.length) || !cif_bytes(c, 1)) {
return false;
}
buffer[path.length] = '/';
if (!cif_copy(c, buffer + path.length + 1, CBM_DISCOVER_VENV_MARKERS[m], length + 1)) {
return false;
}
size_t index = SIZE_MAX;
if (!cif_find_file(c, (cbm_inventory_path_t){buffer, path.length + 1 + length}, &index)) {
return false;
}
*venv = index != SIZE_MAX;
}
return true;
}
/* The same decision as discover.c directory_verdict. *config_only: the
* directory is kept for its configuration files only (CONFIG_ONLY_DIRS). */
static bool cif_directory_reason(cif_context *c, size_t parent, cbm_inventory_path_t path,
cbm_inventory_filter_reason_t *reason) {
cbm_inventory_filter_reason_t *reason, bool *config_only) {
const char *name = cif_basename(c->owner, parent, path);
if (!cif_poll(c)) {
return false;
}
bool builtin = cbm_should_skip_dir(name, CBM_MODE_FULL) &&
(cbm_discover_is_safety_core_directory(name) ||
!cbm_discover_path_in_jvm_package((const char *)path.data));
bool venv = false;
if (cbm_discover_venv_candidate(name) && !cif_venv(c, path, &venv)) {
return false;
}
bool safety_name = cbm_discover_is_safety_core_directory(name);
bool named = cbm_should_skip_dir(name, CBM_MODE_FULL) || venv;
bool config = !named && cbm_discover_config_only_dir(name);
bool package = (named || config) && !safety_name &&
cbm_discover_path_in_jvm_package((const char *)path.data);
bool builtin = named && !package;
config = config && !package;
bool suffix = !builtin && cbm_discover_path_has_skip_suffix((const char *)path.data);
bool safety =
(builtin || suffix) && c->owner->cbm_program && cbm_discover_is_safety_core_directory(name);
bool safety = (builtin || suffix) && c->owner->cbm_program && safety_name;
if (!cif_poll(c)) {
return false;
}
if (builtin || suffix) {
if (builtin || suffix || config) {
cbm_ignore_decision_t decision = CBM_IGNORE_NO_OPINION;
if (!safety && !cif_match(c, c->owner->cbm_program, path, true, &decision)) {
return false;
}
if (decision != CBM_IGNORE_REINCLUDED) {
if (decision != CBM_IGNORE_REINCLUDED && !config) {
*reason = builtin ? CBM_INVENTORY_FILTER_REASON_DIRECTORY_BUILTIN
: CBM_INVENTORY_FILTER_REASON_DIRECTORY_SUFFIX;
return true;
}
*config_only = decision != CBM_IGNORE_REINCLUDED;
}
return cif_ignore_reason(c, parent, path, true, reason);
}
@@ -133,6 +172,7 @@ static bool cif_file_entry(cif_context *c, size_t parent, size_t file) {
return false;
}
o->rows[file].reason = reason;
o->rows[file].config_only = o->directories[parent].config_only;
if (reason != CBM_INVENTORY_FILTER_REASON_NONE) {
o->rows[file].disposition = CBM_INVENTORY_FILTER_IGNORED_FILE;
}
@@ -143,11 +183,14 @@ static bool cif_directory_entry(cif_context *c, size_t parent, size_t directory)
cbm_inventory_filter_t *o = c->owner;
c->file_index = SIZE_MAX;
cbm_inventory_filter_reason_t reason = CBM_INVENTORY_FILTER_REASON_NONE;
if (!cif_directory_reason(c, parent, o->directories[directory].path, &reason) ||
bool config_only = false;
if (!cif_directory_reason(c, parent, o->directories[directory].path, &reason, &config_only) ||
!cif_bytes(c, 128)) {
return false;
}
o->directories[directory].reason = reason;
/* Parents are filtered first, so the inherited flag is final here. */
o->directories[directory].config_only = o->directories[parent].config_only || config_only;
if (reason != CBM_INVENTORY_FILTER_REASON_NONE) {
o->directories[directory].excluded = directory;
}
+3
View File
@@ -113,6 +113,9 @@ typedef struct {
cbm_inventory_filter_disposition_t disposition;
cbm_inventory_filter_reason_t reason;
unsigned roles;
/* In or below a config-only directory (cbm_discover_config_only_dir): the
* language step keeps the file only for a configuration language. */
bool config_only;
cbm_inventory_path_t excluded_ancestor; /* Otherwise {NULL,0}. */
} cbm_inventory_filter_row_t;
+1
View File
@@ -11,6 +11,7 @@ typedef struct {
cbm_inventory_path_t path;
size_t parent, next, first_directory, last_directory, first_file, last_file;
size_t excluded;
bool config_only; /* in or below deploy/ or deployed/: configuration languages only */
cbm_inventory_filter_reason_t reason;
const cbm_ignore_program_t *program;
} cif_directory;
+45
View File
@@ -1084,6 +1084,51 @@ const char *cbm_language_name(CBMLanguage lang) {
return LANG_NAMES[lang] ? LANG_NAMES[lang] : "Unknown";
}
/* Build and configuration languages: they have no cross-language call
* semantics (registry.c's cross-language guard). */
bool cbm_language_is_build_config(CBMLanguage lang) {
return lang == CBM_LANG_MAKEFILE || lang == CBM_LANG_CMAKE || lang == CBM_LANG_YAML ||
lang == CBM_LANG_TOML || lang == CBM_LANG_JSON || lang == CBM_LANG_INI ||
lang == CBM_LANG_DOCKERFILE;
}
/* The languages kept in a deploy/ or deployed/ directory (discover.c
* CONFIG_ONLY_DIRS), by the classification above (name, then shebang): the
* build and configuration languages, every other configuration and
* infrastructure language, and the shell family. General-purpose program
* languages (the build copies the rule exists for), SQL and Groovy are not
* members; neither are the package manifests (cbm_discover_is_package_manifest),
* which discovery drops there by name whatever their language. */
bool cbm_language_is_deployment_config(CBMLanguage lang) {
if (cbm_language_is_build_config(lang)) {
return true;
}
switch (lang) {
/* other configuration and infrastructure languages */
case CBM_LANG_PROPERTIES: /* .properties */
case CBM_LANG_XML: /* web.xml, server and logging configuration */
case CBM_LANG_JSON5:
case CBM_LANG_JSONNET: /* .jsonnet, .libsonnet */
case CBM_LANG_JINJA2: /* .j2: Ansible and Salt templates */
case CBM_LANG_KUSTOMIZE:
case CBM_LANG_GOTEMPLATE: /* Helm template helpers (_helpers.tpl), .gotmpl */
case CBM_LANG_HCL: /* Terraform, OpenTofu, Packer, Nomad, Terragrunt */
case CBM_LANG_BICEP:
case CBM_LANG_NIX:
case CBM_LANG_PUPPET:
case CBM_LANG_PKL:
case CBM_LANG_DOTENV: /* .env, .env.*, *.env */
/* the shell family, by suffix or by shebang */
case CBM_LANG_BASH:
case CBM_LANG_ZSH:
case CBM_LANG_FISH:
case CBM_LANG_POWERSHELL:
return true;
default:
return false;
}
}
/* ── Shebang interpreter detection (extensionless scripts) ────────── */
/* Basename of an interpreter path: the segment after the last '/'. Shebangs
+11 -8
View File
@@ -581,7 +581,8 @@ static const tool_def_t TOOLS[] = {
"\"relationship\":{\"type\":\"string\"},\"min_degree\":{\"type\":\"integer\"},"
"\"max_degree\":{\"type\":\"integer\"},\"exclude_entry_points\":{\"type\":\"boolean\"},"
"\"include_connected\":{\"type\":\"boolean\"},\"semantic_query\":{"
"\"type\":\"array\",\"items\":{\"type\":\"string\"},\"description\":\"Not with query.\"},"
"\"type\":\"array\",\"items\":{\"type\":\"string\"},\"description\":\"[] ignored; "
"otherwise omit query.\"},"
"\"semantic_limit\":{\"type\":\"integer\",\"default\":50,\"minimum\":0,"
"\"maximum\":500},"
"\"semantic_offset\":{\"type\":\"integer\",\"default\":0,\"minimum\":0,"
@@ -4794,7 +4795,7 @@ static sq_run_status_t run_semantic_query_core(const char *args, cbm_store_t *st
yyjson_doc *args_doc = yyjson_read(args, strlen(args), 0);
yyjson_val *args_root = args_doc ? yyjson_doc_get_root(args_doc) : NULL;
yyjson_val *sq_val = args_root ? yyjson_obj_get(args_root, "semantic_query") : NULL;
if (out_present && sq_val) {
if (out_present && sq_val && (!yyjson_is_arr(sq_val) || yyjson_arr_size(sq_val) > 0)) {
*out_present = true;
}
sq_run_status_t status = SQ_RUN_OK;
@@ -4824,10 +4825,11 @@ static sq_run_status_t run_semantic_query_core(const char *args, cbm_store_t *st
return status;
}
static bool search_graph_arg_present(const char *args, const char *name) {
static bool search_graph_semantic_query_present(const char *args) {
yyjson_doc *doc = yyjson_read(args, strlen(args), 0);
yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
bool present = root && yyjson_is_obj(root) && yyjson_obj_get(root, name) != NULL;
yyjson_val *sq_val = root ? yyjson_obj_get(root, "semantic_query") : NULL;
bool present = sq_val && (!yyjson_is_arr(sq_val) || yyjson_arr_size(sq_val) > 0);
if (doc) {
yyjson_doc_free(doc);
}
@@ -5619,13 +5621,14 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
* and return early. If FTS5 is unavailable or the query is empty after
* tokenization, fall through to the regex path. */
char *query = cbm_mcp_get_string_arg(args, "query");
if (query && search_graph_arg_present(args, "semantic_query")) {
if (query && search_graph_semantic_query_present(args)) {
free(query);
free(project);
return cbm_mcp_text_result(
"query and semantic_query are mutually exclusive — use query for BM25 full-text "
"ranking or semantic_query for vector ranking, then issue a separate request for "
"the other mode.",
"query and semantic_query are mutually exclusive. For BM25 search, remove the "
"semantic_query key (an empty array is also accepted). For vector search, remove the "
"query key entirely. To run both modes, send two separate requests; do not retry "
"unchanged arguments.",
true);
}
if (query && query[0]) {
+5
View File
@@ -142,6 +142,11 @@ static bool cls_run(cni_context *c, const cbm_inventory_filter_view_t *view,
if (!cls_one(c, &reader, config, budget, ledger, row->file_index, &language)) {
return false;
}
if (row->config_only &&
(!cbm_language_is_deployment_config(language) ||
cbm_discover_is_package_manifest(cls_basename(&view->files[row->file_index].path)))) {
language = CBM_LANG_COUNT; /* as discovery: program source and manifests in deploy/ */
}
ledger->rows[ledger->count++] =
(cbm_test_impact_classified_t){.file_index = row->file_index, .language = language};
}
+5 -3
View File
@@ -2091,8 +2091,9 @@ static bool jr_scope_open(const jr_ctx_t *c) {
/* True when package `pkg` lies in a directory the indexer never reads, below
* a package the repository owns. A JVM package is a directory path, and
* discovery skips some directory names wherever they occur (`env`, `target`,
* `tmp` ...): what is declared there is on disk, but not in the graph. */
* discovery skips some directory names wherever they occur (`target`, `tmp`
* ..., and the program source of `deploy`): what is declared there is on disk,
* but not in the graph. */
static bool jr_not_indexed(const jr_index_t *ix, const char *pkg) {
char buf[JR_KEY_BUF];
size_t n = strlen(pkg);
@@ -2108,7 +2109,8 @@ static bool jr_not_indexed(const jr_index_t *ix, const char *pkg) {
}
char keep = buf[i];
buf[i] = '\0'; /* buf: the prefix through this segment; buf + seg: the segment */
if (owned && cbm_should_skip_dir(buf + seg, (cbm_index_mode_t)ix->mode)) {
if (owned && (cbm_should_skip_dir(buf + seg, (cbm_index_mode_t)ix->mode) ||
cbm_discover_config_only_dir(buf + seg))) {
return true;
}
owned = owned || cbm_ht_get(ix->packages, buf) || cbm_ht_get(ix->pkg_prefixes, buf);
+245 -5
View File
@@ -608,6 +608,202 @@ const char *cbm_lsp_surface_identity_decline(const char *const *old_rows,
return decline;
}
/* ── Scope facts: the "is" key ──────────────────────────────────────────
*
* A file's scope facts (cbm.h, CBMScopeEvent), stored as
* "is": {"v": SURFACE_SCOPE_V, "ev": [[kind, flags, rank, ...], ...], "im": [module path, ...]}
* where rank orders the stored events by position (equal positions share a
* rank): an edit that moves bytes but keeps the facts keeps the stored bytes,
* and so the surface hash. Per kind: DEF [.., name, qualified name, qual],
* INCLUDE [.., index into "im"], ASSIGN [.., name], OPAQUE [..]. QREF events
* are not stored: a reference is read where its own file is re-extracted.
* Written only for a language with a scope row, so every other file's bytes
* stay as they were. A row of such a language written by an older binary has
* no "is": a reader must take its facts as unknown, never as empty. */
enum { SURFACE_SCOPE_V = 1 };
static bool surface_scope_stored(const CBMFileResult *r, const CBMScopeEvent *e) {
switch (e->kind) {
case CBM_SCOPE_INCLUDE:
return e->index >= 0 && e->index < r->imports.count &&
r->imports.items[e->index].module_path;
case CBM_SCOPE_DEF:
return e->index >= 0 && e->index < r->defs.count && e->name &&
r->defs.items[e->index].qualified_name;
case CBM_SCOPE_ASSIGN:
return e->name != NULL;
case CBM_SCOPE_OPAQUE:
return true;
default:
return false;
}
}
static bool surface_arr_add_str_or_null(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *s) {
return s ? yyjson_mut_arr_add_strcpy(doc, arr, s) : yyjson_mut_arr_add_null(doc, arr);
}
/* One stored event into `ev` (its module path into `im`). */
static bool surface_scope_event(yyjson_mut_doc *doc, yyjson_mut_val *ev, yyjson_mut_val *im,
int *nim, const CBMFileResult *r, const CBMScopeEvent *e,
int rank) {
yyjson_mut_val *a = yyjson_mut_arr(doc);
bool ok = a && yyjson_mut_arr_add_int(doc, a, e->kind) &&
yyjson_mut_arr_add_int(doc, a, e->flags) && yyjson_mut_arr_add_int(doc, a, rank);
switch (e->kind) {
case CBM_SCOPE_INCLUDE:
ok = ok && yyjson_mut_arr_add_strcpy(doc, im, r->imports.items[e->index].module_path) &&
yyjson_mut_arr_add_int(doc, a, (*nim)++);
break;
case CBM_SCOPE_DEF:
ok = ok && yyjson_mut_arr_add_strcpy(doc, a, e->name) &&
yyjson_mut_arr_add_strcpy(doc, a, r->defs.items[e->index].qualified_name) &&
surface_arr_add_str_or_null(doc, a, e->qual);
break;
case CBM_SCOPE_ASSIGN:
ok = ok && yyjson_mut_arr_add_strcpy(doc, a, e->name);
break;
default:
break;
}
return ok && yyjson_mut_arr_add_val(ev, a);
}
static bool surface_add_scope(yyjson_mut_doc *doc, yyjson_mut_val *root, const CBMFileResult *r,
CBMLanguage language) {
if (!cbm_extract_scope_has_row(language)) {
return true; /* no row: nothing to write */
}
yyjson_mut_val *is = yyjson_mut_obj(doc);
yyjson_mut_val *ev = yyjson_mut_arr(doc);
yyjson_mut_val *im = yyjson_mut_arr(doc);
if (!is || !ev || !im) {
return false;
}
int rank = -1;
int64_t last = -1;
int nim = 0;
for (int i = 0; r && i < r->scope.count; i++) {
const CBMScopeEvent *e = &r->scope.items[i];
if (!surface_scope_stored(r, e)) {
continue;
}
if ((int64_t)e->pos != last) {
rank++;
last = e->pos;
}
if (!surface_scope_event(doc, ev, im, &nim, r, e, rank)) {
return false;
}
}
return yyjson_mut_obj_add_int(doc, is, "v", SURFACE_SCOPE_V) &&
yyjson_mut_obj_add_val(doc, is, "ev", ev) && yyjson_mut_obj_add_val(doc, is, "im", im) &&
yyjson_mut_obj_add_val(doc, root, "is", is);
}
static const char *surface_json_strdup(CBMArena *a, yyjson_val *v) {
const char *s = v ? yyjson_get_str(v) : NULL;
return s ? cbm_arena_strdup(a, s) : NULL;
}
/* One stored event into e (its definition into defs[*ndefs] for a DEF);
* false: malformed. */
static bool surface_scope_event_parse(CBMArena *a, yyjson_val *item, size_t nim, CBMScopeEvent *e,
CBMDefinition *defs, int *ndefs) {
if (!yyjson_is_arr(item) || yyjson_arr_size(item) < 3) {
return false;
}
yyjson_val *kind = yyjson_arr_get(item, 0);
yyjson_val *flags = yyjson_arr_get(item, 1);
yyjson_val *rank = yyjson_arr_get(item, 2);
if (!yyjson_is_int(kind) || !yyjson_is_int(flags) || !yyjson_is_int(rank) ||
yyjson_get_int(rank) < 0) {
return false;
}
memset(e, 0, sizeof(*e));
e->kind = (uint8_t)yyjson_get_int(kind);
e->flags = (uint8_t)yyjson_get_int(flags);
e->pos = (uint32_t)yyjson_get_int(rank);
e->index = -1;
yyjson_val *x = yyjson_arr_get(item, 3);
switch (e->kind) {
case CBM_SCOPE_INCLUDE:
e->index = (int32_t)yyjson_get_int(x);
return yyjson_is_int(x) && e->index >= 0 && (size_t)e->index < nim;
case CBM_SCOPE_DEF: {
CBMDefinition *d = &defs[*ndefs];
memset(d, 0, sizeof(*d));
e->name = surface_json_strdup(a, x);
e->qual = surface_json_strdup(a, yyjson_arr_get(item, 5));
d->name = e->name;
d->qualified_name = surface_json_strdup(a, yyjson_arr_get(item, 4));
e->index = (*ndefs)++;
return e->name && d->qualified_name;
}
case CBM_SCOPE_ASSIGN:
e->name = surface_json_strdup(a, x);
return e->name != NULL;
case CBM_SCOPE_OPAQUE:
return true;
default:
return false;
}
}
int cbm_lsp_surface_scope_from_json(CBMArena *arena, const char *json, cbm_lsp_scope_facts_t *out) {
if (!out) {
return CBM_NOT_FOUND;
}
memset(out, 0, sizeof(*out));
if (!arena || !json) {
return CBM_NOT_FOUND;
}
yyjson_doc *jd = yyjson_read(json, strlen(json), 0);
yyjson_val *root = jd ? yyjson_doc_get_root(jd) : NULL;
yyjson_val *is = root ? yyjson_obj_get(root, "is") : NULL;
if (!is) {
yyjson_doc_free(jd);
return jd ? 0 : CBM_NOT_FOUND;
}
yyjson_val *ver = yyjson_obj_get(is, "v");
yyjson_val *ev = yyjson_obj_get(is, "ev");
yyjson_val *im = yyjson_obj_get(is, "im");
bool ok = yyjson_is_int(ver) && yyjson_get_int(ver) == SURFACE_SCOPE_V && yyjson_is_arr(ev) &&
yyjson_is_arr(im);
size_t nev = ok ? yyjson_arr_size(ev) : 0;
size_t nim = ok ? yyjson_arr_size(im) : 0;
CBMScopeEvent *events = nev ? cbm_arena_alloc(arena, nev * sizeof(*events)) : NULL;
CBMDefinition *defs = nev ? cbm_arena_alloc(arena, nev * sizeof(*defs)) : NULL;
const char **modules = nim ? cbm_arena_alloc(arena, nim * sizeof(*modules)) : NULL;
ok = ok && (!nev || (events && defs)) && (!nim || modules);
size_t idx;
size_t max;
yyjson_val *item;
if (ok) {
yyjson_arr_foreach(im, idx, max, item) {
modules[idx] = surface_json_strdup(arena, item);
ok = ok && modules[idx];
}
}
int ndefs = 0;
if (ok) {
yyjson_arr_foreach(ev, idx, max, item) {
ok = ok && surface_scope_event_parse(arena, item, nim, &events[idx], defs, &ndefs);
}
}
yyjson_doc_free(jd);
if (!ok) {
return CBM_NOT_FOUND;
}
*out = (cbm_lsp_scope_facts_t){.events = events,
.event_count = (int)nev,
.defs = defs,
.def_count = ndefs,
.modules = modules,
.module_count = (int)nim};
return SKIP_ONE;
}
/* Serialize one file's surface: its slice of all_defs plus the registry-only
* symbols from its raw extraction defs. Returns the JSON string (malloc'd, or
* from `alc` when given) and its length. With identities, each def's
@@ -752,6 +948,10 @@ static char *surface_file_to_json(const CBMFileResult *result, CBMLanguage langu
yyjson_mut_doc_free(doc);
return NULL;
}
if (!surface_add_scope(doc, root, result, language)) {
yyjson_mut_doc_free(doc);
return NULL; /* missing facts would diverge silently: fail the row */
}
/* Names, types and strings come from source text, which need not be UTF-8:
* one such byte must not cost the repository every surface row. */
char *json = cbm_json_write_text(doc, alc, out_len);
@@ -775,6 +975,42 @@ typedef struct {
_Atomic bool failed;
} surface_job_t;
/* Row strings are released with cbm_store_free_lsp_surfaces (free), so they
* come from the C heap: one place allocates them. */
static char *surface_row_alloc(size_t n) {
return malloc(n);
}
static char *surface_row_strdup(const char *s) {
size_t n = strlen(s);
char *copy = surface_row_alloc(n + 1);
if (copy) {
memcpy(copy, s, n + 1);
}
return copy;
}
/* CBMFileResult.included_files, one file per line ("" when none). */
static char *surface_row_included_files(const CBMFileResult *fr) {
size_t len = 0;
for (const char *const *f = fr->included_files; f && *f; f++) {
len += strlen(*f) + 1;
}
char *out = surface_row_alloc(len + 1);
if (!out) {
return NULL;
}
size_t at = 0;
for (const char *const *f = fr->included_files; f && *f; f++) {
size_t n = strlen(*f);
memcpy(out + at, *f, n);
at += n;
out[at++] = '\n';
}
out[at] = '\0';
return out;
}
static void surface_row_build(int i, void *arg) {
surface_job_t *job = (surface_job_t *)arg;
if (atomic_load_explicit(&job->failed, memory_order_relaxed)) {
@@ -814,19 +1050,23 @@ static void surface_row_build(int i, void *arg) {
cbm_free(CBM_MEM_CLASS_OTHER, json);
json = stored;
}
char *included = surface_row_included_files(fr);
cbm_pipeline_result_release(fr, loaded);
if (!json) {
if (!json || !included) {
free(json);
free(included);
atomic_store_explicit(&job->failed, true, memory_order_relaxed);
return;
}
cbm_lsp_surface_row_t *r = &job->rows[i];
r->defs_json = json; /* set first: it marks the row present for the compaction */
r->project = strdup(job->project);
r->rel_path = strdup(job->files[i].rel_path);
r->surface_sha = strdup(sha);
r->project = surface_row_strdup(job->project);
r->rel_path = surface_row_strdup(job->files[i].rel_path);
r->surface_sha = surface_row_strdup(sha);
r->ref_bloom = NULL;
r->ref_bloom_len = 0;
r->config_ctx = strdup("");
r->config_ctx = surface_row_strdup("");
r->included_files = included;
if (!r->project || !r->rel_path || !r->surface_sha || !r->config_ctx) {
atomic_store_explicit(&job->failed, true, memory_order_relaxed);
}
+18
View File
@@ -60,6 +60,24 @@ typedef struct {
int cbm_lsp_surface_python_namespace_from_json(CBMArena *arena, const char *json,
cbm_lsp_python_namespace_t *out);
/* A stored file's scope facts (the "is" key, see lsp_surface.c). Events keep
* their kind and flags; pos is the stored rank (source order, equal positions
* equal); a DEF's index is into defs (name, qualified_name), an INCLUDE's
* into modules (module paths). QREF events are not stored. */
typedef struct {
CBMScopeEvent *events;
int event_count;
CBMDefinition *defs;
int def_count;
const char **modules;
int module_count;
} cbm_lsp_scope_facts_t;
/* Returns 1 with the row's facts copied into `arena`, 0 for a row without
* "is" (a language without a scope row, or a row an older binary wrote), -1
* for a malformed row or another "is" version. */
int cbm_lsp_surface_scope_from_json(CBMArena *arena, const char *json, cbm_lsp_scope_facts_t *out);
/* Conservative namespace admission for incremental planning. Both rows must
* have a current, valid fact envelope before any equality shortcut is used.
* Returns -1 for invalid/incomplete metadata, 0 when both rows are non-Python
+37 -19
View File
@@ -28,6 +28,7 @@ static const double PC_SWIFT_AMBIGUOUS_CONF = 0.55;
#include "pipeline/pipeline_internal.h"
#include "pipeline/lsp_resolve.h"
#include "pipeline/laravel_routing.h"
#include "pipeline/site_bind.h"
#include "graph_buffer/graph_buffer.h"
#include "foundation/log.h"
#include "foundation/compat.h"
@@ -1061,15 +1062,18 @@ static bool calls_emit_lsp_target(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
/* Vendor receivers, Java library receivers and cross-language name guesses
* cannot bind project calls. Keep the registry policy shared with the
* parallel resolver. `caller_test`: cbm_pipeline_caller_is_test of the
* calling definition. */
static cbm_resolution_t calls_registry_resolution(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
const char *module_qn, const char **imp_keys,
const char **imp_vals, int imp_count,
CBMLanguage lang, bool rust_external,
bool php_vendor_call, bool java_external,
bool caller_test) {
* calling definition. `scoped`: the shared layer's answer at this site
* (site_bind.h); a proven one takes the place of the name chain. */
static cbm_resolution_t calls_registry_resolution(
cbm_pipeline_ctx_t *ctx, const CBMCall *call, const char *module_qn, const char **imp_keys,
const char **imp_vals, int imp_count, CBMLanguage lang, bool rust_external,
bool php_vendor_call, bool java_external, bool caller_test, const cbm_site_answer_t *scoped) {
cbm_resolution_t res = {0};
if (!rust_external && !php_vendor_call && !java_external) {
/* MUST match pass_parallel.c. */
if (scoped->verdict == CBM_SITE_PROVEN) {
return scoped->res;
}
/* Python LEGB fallback first (MUST match pass_parallel.c). */
res = cbm_registry_resolve_call_scoped(ctx->registry, call->callee_name, module_qn,
imp_keys, imp_vals, imp_count, lang,
@@ -1118,8 +1122,8 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
const char **imp_vals, int imp_count, CBMLanguage lang,
const CBMImportArray *imports, const char *route_mount,
const cbm_php_vendor_names_t *php_vendor,
cbm_external_import_index_t *ext_imports,
CBMCallEvidence *evidence) {
cbm_external_import_index_t *ext_imports, CBMCallEvidence *evidence,
cbm_site_file_t *site) {
const CBMResolvedCallArray *lsp_calls = &result->resolved_calls;
const cbm_gbuf_node_t *source_node = calls_find_source(ctx, rel, call->enclosing_func_qn);
if (!source_node) {
@@ -1155,6 +1159,15 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
return 0;
}
/* The shared cross-file layer (site_bind.h) is asked at every call no
* engine row matched, here before the service classification so both
* routes ask the same sites; a proven answer replaces the name chain
* below. MUST match pass_parallel.c. */
cbm_site_answer_t scoped = {.verdict = CBM_SITE_NO_ANSWER};
if (!lsp) {
scoped = cbm_pipeline_site_bind_call(site, call, source_node);
}
/* Service-pattern HTTP/ASYNC client call (`requests.get(url)`): the service
* signal lives in the callee_name. The registry can mis-resolve such a call
* to a spurious builtin short-name match (e.g. `requests.get` ->
@@ -1240,9 +1253,10 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
lang, lsp->strategy, lsp->callee_qn, ctx->project_name);
cbm_resolution_t res = {0};
if (swift_candidates >= 0) {
res = calls_registry_resolution(ctx, call, module_qn, imp_keys, imp_vals, imp_count, lang,
rust_external, php_vendor_call, java_external,
cbm_pipeline_caller_is_test(source_node, rel, lang));
res =
calls_registry_resolution(ctx, call, module_qn, imp_keys, imp_vals, imp_count, lang,
rust_external, php_vendor_call, java_external,
cbm_pipeline_caller_is_test(source_node, rel, lang), &scoped);
}
/* `p->open(fd)`: the registry does not split a callee on the arrow, and a
* member is not a name it could resolve anyway. The object's type decides,
@@ -1545,12 +1559,13 @@ static CBMFileResult *calls_get_or_extract(cbm_pipeline_ctx_t *ctx, int idx,
}
return NULL;
}
CBMFileResult *r =
cbm_pipeline_test_force_extract_null(ctx, fi->language)
? NULL
: cbm_extract_file_ex_with_tests(
src, slen, fi->language, ctx->project_name, fi->rel_path, CBM_EXTRACT_BUDGET,
NULL, NULL, ctx->macro_table, ctx->return_type_table, ctx->test_declarations);
CBMIncludeScope include_scope = cbm_pipeline_include_scope(ctx);
CBMFileResult *r = cbm_pipeline_test_force_extract_null(ctx, fi->language)
? NULL
: cbm_extract_file_ex_with_tests(
src, slen, fi->language, ctx->project_name, fi->rel_path,
CBM_EXTRACT_BUDGET, NULL, NULL, ctx->macro_table,
ctx->return_type_table, ctx->test_declarations, &include_scope);
free(src);
(void)cbm_pipeline_test_extraction_ok(ctx, fi->language, r);
if (r) {
@@ -1631,6 +1646,8 @@ 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);
cbm_site_file_t site;
cbm_pipeline_site_bind_begin(&site, ctx, ctx->gbuf, rel, files[i].language, result);
for (int c = 0; c < result->calls.count; c++) {
CBMCall *call = &result->calls.items[c];
if (!call->callee_name) {
@@ -1641,12 +1658,13 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
if (resolve_single_call(ctx, call, result, rel, module_qn, imp_keys, imp_vals,
imp_count, files[i].language, &result->imports, route_mount,
&php_vendor, &ext_imports,
calls_evidence_at(evidence, c, &fallback))) {
calls_evidence_at(evidence, c, &fallback), &site)) {
resolved++;
} else {
unresolved++;
}
}
cbm_pipeline_site_bind_end(&site, ctx->gbuf);
cbm_spawn_file_end();
cbm_pipeline_external_import_index_free(&ext_imports);
+5 -4
View File
@@ -1098,15 +1098,16 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
/* Studio Export XML is transformed to one cacheable aggregate so later
* passes see the same calls/usages/semantic carriers as native UDL. */
CBMIncludeScope include_scope = cbm_pipeline_include_scope(ctx);
CBMFileResult *result =
cbm_pipeline_test_force_extract_null(ctx, lang) ? NULL
: lang == CBM_LANG_OBJECTSCRIPT_EXPORT
? cbm_pipeline_extract_objectscript_export(source, source_len, ctx->project_name,
rel, ctx->macro_table, NULL)
: cbm_extract_file_ex_with_tests(source, source_len, lang, ctx->project_name, rel,
CBM_EXTRACT_BUDGET, NULL,
NULL /* no extra defines or include paths */,
ctx->macro_table, NULL, ctx->test_declarations);
: cbm_extract_file_ex_with_tests(
source, source_len, lang, ctx->project_name, rel, CBM_EXTRACT_BUDGET, NULL,
NULL /* no extra defines or include paths */, ctx->macro_table, NULL,
ctx->test_declarations, &include_scope);
free(source);
if (!cbm_pipeline_test_extraction_ok(ctx, lang, result)) {
File diff suppressed because it is too large Load Diff
+68 -64
View File
@@ -12,9 +12,10 @@
* so the same cbm_pipeline_find_lsp_resolution path that handles per-
* file LSP picks them up.
*
* Languages covered: Go, C/C++/CUDA, Python, TypeScript/JavaScript/JSX/
* TSX, PHP, C#, and JVM (Java/Kotlin via the shared filter helper).
* Anything else short-circuits via cbm_pxc_has_cross_lsp.
* The languages covered are the rows of one engine table (pass_lsp_cross.c
* pxc_engines): Go, C/C++/CUDA, Python, TypeScript/JavaScript/JSX/TSX, PHP,
* C#, Java, Kotlin and Rust. Anything else short-circuits via
* cbm_pxc_has_cross_lsp.
*
* Previously this work ran as a separate sequential pipeline pass
* (cbm_pipeline_pass_lsp_cross) that re-read every source file from
@@ -38,10 +39,43 @@
#include "lsp/java_lsp.h" /* cbm_java_build_cross_registry / cbm_run_java_lsp_cross_with_registry */
#include "lsp/rust_lsp.h" /* cbm_rust_build_cross_registry / cbm_run_rust_lsp_cross_with_registry */
#include "pipeline/pipeline_internal.h"
#include "pipeline/type_scope.h" /* cbm_type_scope_file_t */
#include <stdbool.h>
/* True iff this language has a cbm_run_X_lsp_cross resolver wired up. */
/* ── The engine table ──
*
* One row per type-aware engine (pass_lsp_cross.c pxc_engines): the
* languages it serves, the shared registry it builds once over all defs, its
* walk of one file against a registry, its walk of one file over a def list,
* and whether it reads declared-type identities (type_scope.h). Every
* per-language decision of the cross-file pass is a field of that row; an
* engine plugs in by adding its row. A row's index is its engine number. */
enum { CBM_PXC_MAX_ENGINES = 16 };
/* Number of rows; a row's name ("go", "python", ...; NULL out of range); the
* row that serves `lang` (-1: none). */
int cbm_pxc_engine_count(void);
const char *cbm_pxc_engine_name(int engine);
int cbm_pxc_engine_of_lang(CBMLanguage lang);
/* True iff this language has an engine row. */
bool cbm_pxc_has_cross_lsp(CBMLanguage lang);
/* True iff the language's engine reads declared-type identities: the
* collector attaches them (cbm_type_scope_lang must have the language's
* row) and the per-file def selection follows them. */
bool cbm_pxc_wants_identities(CBMLanguage lang);
/* One file's declared-type scope, exactly as the collector builds it to
* attach the file's identities (the project registry, the file's module and
* LSP import map, and for a lexical row the file's definitions and module
* facts): a walk reads the calling file's own identity table from it
* (cbm_type_scope_file_imports). NULL without a pipeline context or for an
* engine that reads no identities; else end it with cbm_type_scope_file_end
* once the walk is done. */
cbm_type_scope_file_t *cbm_pxc_file_scope(const cbm_pipeline_ctx_t *ctx, CBMLanguage lang,
const CBMFileResult *result, const char *module_qn,
const char **imp_keys, const char **imp_vals,
int imp_count);
/* Collect a project-wide CBMLSPDef[] from every cached file result.
* def_modules[i] receives the module QN for files[i] (malloc'd; the
@@ -140,28 +174,33 @@ CBMLSPDef *cbm_pxc_filter_defs_for_file(const CBMModuleDefIndex *idx, CBMLSPDef
uint64_t cbm_pxc_test_reach_work(void);
#endif
/* ── Tier 2 full: pre-built per-language cross-LSP registries ─────
/* ── Tier 2 full: pre-built per-engine cross-LSP registries ─────
*
* Each non-NULL registry is built ONCE in pipeline.c (in a dedicated
* cross_lsp_arena), finalized, and shared READ-ONLY across all
* resolve workers for files of that language. The worker uses the
* matching cbm_run_X_lsp_cross_with_registry variant which skips the
* per-file registry build entirely. NULL → fall back to the per-file
* cbm_pxc_run_one path. */
* Each non-NULL registry is built ONCE per run (in a dedicated arena),
* finalized, and shared READ-ONLY across all resolve workers for files of
* that engine's languages. The walk then runs against a per-file overlay
* chained to it and skips the per-file registry build entirely. NULL → the
* per-file path. A row whose registry is lazy (Rust) is not built here: the
* parallel resolver builds it on the first file that needs it. */
typedef struct {
CBMTypeRegistry *go; /* CBM_LANG_GO */
CBMTypeRegistry *c; /* CBM_LANG_C, CBM_LANG_CPP, CBM_LANG_CUDA */
CBMTypeRegistry *python; /* CBM_LANG_PYTHON */
CBMTypeRegistry *ts; /* CBM_LANG_JAVASCRIPT, TYPESCRIPT, TSX */
CBMTypeRegistry *php; /* CBM_LANG_PHP */
CBMTypeRegistry *cs; /* CBM_LANG_CSHARP */
CBMTypeRegistry *java; /* CBM_LANG_JAVA (JVM def universe incl. Kotlin defs) */
/* CBM_LANG_RUST: intentionally absent — the shared rust registry is built
* LAZILY inside cbm_parallel_resolve (first NULL-filter rust file), not eagerly. */
CBMTypeRegistry *by_engine[CBM_PXC_MAX_ENGINES]; /* indexed by engine row */
/* The pipeline context the walks may read (the project registry, for the
* calling file's own identities: cbm_pxc_file_scope). Every driver sets
* it, also when it builds no registry; NULL in a harness without one. */
const cbm_pipeline_ctx_t *ctx;
} CBMCrossLspRegistries;
/* Return the appropriate pre-built registry for a language, or NULL
* if none was built (or language has no cross-LSP entrypoint). */
/* Build one row's shared registry over all defs into out->by_engine[engine].
* False (slot untouched) for a row without an eagerly built registry. */
bool cbm_pxc_build_engine_registry(int engine, CBMArena *arena, CBMLSPDef *defs, int def_count,
CBMCrossLspRegistries *out);
/* Every row's, in row order. */
void cbm_pxc_build_cross_registries(CBMArena *arena, CBMLSPDef *defs, int def_count,
CBMCrossLspRegistries *out);
/* The pre-built registry of the engine serving `lang`, or NULL if none was
* built (or the language has no engine; r may be NULL). */
CBMTypeRegistry *cbm_pxc_registry_for_lang(const CBMCrossLspRegistries *r, CBMLanguage lang);
/* Per-file registry-build cost (#1669): how many defs the per-file cross-LSP
* path actually registered, and how often the module filter failed. */
/* Count defs an overlay registered for one file (complexity-gate telemetry). */
@@ -170,35 +209,6 @@ void cbm_pxc_count_perfile_defs(uint64_t defs);
void cbm_pxc_filter_stats(uint64_t *defs_registered, uint64_t *build_files, uint64_t *filter_files,
uint64_t *filter_failed);
static inline CBMTypeRegistry *cbm_pxc_registry_for_lang(const CBMCrossLspRegistries *r,
CBMLanguage lang) {
if (!r) {
return NULL;
}
switch (lang) {
case CBM_LANG_GO:
return r->go;
case CBM_LANG_C: /* fallthrough */
case CBM_LANG_CPP: /* fallthrough */
case CBM_LANG_CUDA:
return r->c;
case CBM_LANG_PYTHON:
return r->python;
case CBM_LANG_JAVASCRIPT: /* fallthrough */
case CBM_LANG_TYPESCRIPT: /* fallthrough */
case CBM_LANG_TSX:
return r->ts;
case CBM_LANG_PHP:
return r->php;
case CBM_LANG_CSHARP:
return r->cs;
case CBM_LANG_JAVA:
return r->java;
default:
return NULL; /* incl. CBM_LANG_RUST — its shared registry is built lazily */
}
}
/* Build and borrow the Rust Cargo manifest used for cross-crate (#56) routing.
* The manifest owns strings in manifest_arena; callers keep that arena alive
* until every resolver worker has joined. Each worker must install the shared
@@ -213,8 +223,8 @@ void cbm_pxc_thread_scratch_begin(void);
void cbm_pxc_thread_scratch_end(void);
const struct CBMCargoManifest *cbm_pxc_get_rust_manifest(void);
/* Run the cross-file LSP resolver for non-TS languages. Appends
* resolved CALLS into r->resolved_calls (lives in r->arena). Caller
/* Run the engine's per-file cross-file walk for one file over `defs`.
* Appends resolved CALLS into r->resolved_calls (lives in r->arena). Caller
* owns source, module_qn, defs, imp_names, imp_qns.
* NOTE: defs is read-only in practice but typed non-const to match
* the existing cbm_run_X_lsp_cross callee signatures. */
@@ -231,18 +241,12 @@ void cbm_pxc_run_one_with_php_bindings(CBMLanguage lang, CBMFileResult *r, const
const char **imp_qns, int imp_count,
const CBMPHPImportBindings *bindings);
/* TS / JS / JSX / TSX variant with explicit dialect flags. */
void cbm_pxc_run_one_ts(CBMFileResult *r, const char *source, int source_len, const char *module_qn,
CBMLSPDef *defs, int def_count, const char **imp_names,
const char **imp_qns, int imp_count, bool js_mode, bool jsx_mode,
bool dts_mode);
/* Per-file cross-LSP dispatch shared by the parallel resolve worker AND the
* sequential driver (one path = one semantics): module-def-index filter →
* shared prebuilt registry (overlay pattern, no per-file registry build) →
* per-file fallback with FILTERED defs for languages without a shared
* variant. rust_shared_get (nullable) supplies the lazily-built shared Rust
* registry for NULL-filter rust files. */
* sequential driver (one path = one semantics): shared prebuilt registry
* (overlay pattern, no per-file registry build) → per-file fallback with
* the module-def-index's FILTERED defs for engines without one.
* rust_shared_get (nullable) supplies the lazily-built shared registry of
* the engine whose registry is lazy (Rust). */
void cbm_pxc_dispatch_file(CBMLanguage lang, CBMFileResult *result, const char *source,
int source_len, const char *rel, const char *def_module,
const CBMCrossLspRegistries *cross_registries,
+34 -6
View File
@@ -79,6 +79,7 @@ enum { PP_CSHARP_M_PREFIX_LEN = 2 };
#include "pipeline/pass_lsp_cross.h" /* cbm_pxc_* helpers for fused cross-file LSP */
#include "pipeline/lsp_resolve.h"
#include "pipeline/laravel_routing.h"
#include "pipeline/site_bind.h"
#include "lsp/rust_cargo.h"
#include "lsp/c_lsp.h"
#include "lsp/py_lsp.h"
@@ -1361,6 +1362,7 @@ static void extract_worker(int worker_id, void *ctx_ptr) {
/* Export XML uses the same cache slot as every physical file, so its
* generated classes are composed before entering the common registry
* and resolution lifecycle. */
CBMIncludeScope include_scope = cbm_pipeline_include_scope(ec->pctx);
CBMFileResult *result =
cbm_pipeline_test_force_extract_null(ec->pctx, fi->language) ? NULL
: fi->language == CBM_LANG_OBJECTSCRIPT_EXPORT
@@ -1370,7 +1372,7 @@ static void extract_worker(int worker_id, void *ctx_ptr) {
: cbm_extract_file_ex_with_tests(source, source_len, fi->language, ec->project_name,
fi->rel_path, CBM_EXTRACT_BUDGET, NULL, NULL,
ec->macro_table, ec->return_type_table,
ec->pctx->test_declarations);
ec->pctx->test_declarations, &include_scope);
uint64_t file_elapsed_ms = (extract_now_ns() - file_t0) / PP_USEC_PER_MS;
@@ -3351,6 +3353,8 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
bool field_index_built = false;
bool field_index_ready = false;
cbm_spawn_file_begin(result->imports.items, result->imports.count);
cbm_site_file_t site;
cbm_pipeline_site_bind_begin(&site, rc->pctx, rc->main_gbuf, rel, lang, result);
for (int c = 0; c < result->calls.count; c++) {
const CBMCall *call = &result->calls.items[c];
CBMCallEvidence fallback = {0};
@@ -3464,12 +3468,23 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
module_qn, imp_vals, imp_count,
candidates, CBM_SZ_256);
}
/* The shared cross-file layer (site_bind.h) is asked at every call no
* engine row matched; a proven answer replaces the name chain. MUST
* match pass_calls.c. */
cbm_site_answer_t scoped = {.verdict = CBM_SITE_NO_ANSWER};
if (!lsp && !call->requires_lsp_resolution) {
scoped = cbm_pipeline_site_bind_call(&site, call, source_node);
}
if ((!res.qualified_name || !res.qualified_name[0]) && !call->requires_lsp_resolution &&
!rust_external && !php_vendor_call && !java_external && count == 0) {
/* Python LEGB fallback first (MUST match pass_calls.c). */
res = cbm_registry_resolve_call_scoped(
rc->registry, call->callee_name, module_qn, imp_keys, imp_vals, imp_count, lang,
call->enclosing_func_qn, cbm_pipeline_caller_is_test(source_node, rel, lang));
if (scoped.verdict == CBM_SITE_PROVEN) {
res = scoped.res;
} else {
/* Python LEGB fallback first (MUST match pass_calls.c). */
res = cbm_registry_resolve_call_scoped(
rc->registry, call->callee_name, module_qn, imp_keys, imp_vals, imp_count, lang,
call->enclosing_func_qn, cbm_pipeline_caller_is_test(source_node, rel, lang));
}
}
/* `p->open(fd)`: the registry does not split a callee on the arrow.
* The object's type decides, as for the dot call handled further
@@ -3822,6 +3837,7 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
memory_order_relaxed);
ws->calls_resolved++;
}
cbm_pipeline_site_bind_end(&site, ws->local_edge_buf);
if (lsp_exact_idx) {
cbm_ht_foreach(lsp_exact_idx, lsp_idx_free_key, NULL);
cbm_ht_free(lsp_exact_idx);
@@ -3846,6 +3862,8 @@ static void resolve_file_usages(resolve_ctx_t *rc, resolve_worker_state_t *ws,
cbm_pipeline_lsp_field_index_t field_index = {0};
bool field_index_ready =
cbm_pipeline_lsp_field_index_build(&result->resolved_calls, &field_index);
cbm_site_file_t site;
cbm_pipeline_site_bind_begin(&site, rc->pctx, rc->main_gbuf, rel, lang, result);
for (int u = 0; u < result->usages.count; u++) {
CBMUsage *usage = &result->usages.items[u];
if (!usage->ref_name) {
@@ -3922,7 +3940,16 @@ static void resolve_file_usages(resolve_ctx_t *rc, resolve_worker_state_t *ws,
precise_call_reference = cbm_pipeline_node_is_callable_target(tgt);
}
}
if (!tgt) {
/* The shared cross-file layer (site_bind.h) is asked at every usage no
* engine row was found for; a proven answer is the target and main's
* name chain does not run for this site. MUST match pass_usages.c. */
cbm_site_answer_t scoped = {.verdict = CBM_SITE_NO_ANSWER};
if (!tgt && !semantic_reference && !property_read) {
scoped = cbm_pipeline_site_bind_usage(&site, usage, src);
}
if (scoped.verdict == CBM_SITE_PROVEN) {
tgt = scoped.target;
} else if (!tgt) {
/* Exact semantic ownership beats textual name fallback even when
* the semantic target is not materialized in this graph. */
if (semantic_reference) {
@@ -4001,6 +4028,7 @@ static void resolve_file_usages(resolve_ctx_t *rc, resolve_worker_state_t *ws,
cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, tgt->id, edge_type, uprops);
ws->usages_resolved++;
}
cbm_pipeline_site_bind_end(&site, ws->local_edge_buf);
cbm_pipeline_lsp_reference_index_free(&reference_index);
cbm_pipeline_lsp_field_index_free(&field_index);
}
+5 -13
View File
@@ -1210,18 +1210,7 @@ CBMHashTable *cbm_pkgmap_build(cbm_pkg_entries_t *worker_entries, int worker_cou
* Used by the filesystem walker; cbm_pkgmap_try_parse is the source of
* truth for which basenames produce entries. */
static bool is_pkgmap_manifest_basename(const char *basename) {
if (!basename) {
return false;
}
if (strcmp(basename, "package.json") == 0 || strcmp(basename, "go.mod") == 0 ||
strcmp(basename, "Cargo.toml") == 0 || strcmp(basename, "pyproject.toml") == 0 ||
strcmp(basename, "composer.json") == 0 || strcmp(basename, "pubspec.yaml") == 0 ||
strcmp(basename, "pom.xml") == 0 || strcmp(basename, "build.gradle") == 0 ||
strcmp(basename, "build.gradle.kts") == 0 || strcmp(basename, "mix.exs") == 0 ||
strcmp(basename, "Package.swift") == 0) {
return true;
}
return ends_with(basename, ".gemspec");
return cbm_discover_is_package_manifest(basename); /* one list, shared with discovery */
}
/* Stat a path, skipping symlinks. Returns 0 on success, -1 to skip.
@@ -1353,7 +1342,10 @@ static int pkgmap_walk_dir(const char *abs_dir, const char *rel_dir, cbm_pkg_ent
continue;
}
if (S_ISDIR(st.st_mode)) {
if (cbm_should_skip_dir(name, CBM_MODE_FULL) ||
/* deploy/ and deployed/ hold no indexed program source (their
* configuration files only), so a package manifest there names
* no package of the graph: skipped, as before. */
if (cbm_should_skip_dir(name, CBM_MODE_FULL) || cbm_discover_config_only_dir(name) ||
cbm_pipeline_relpath_is_excluded(rel_path, excluded_dirs, excluded_count)) {
continue;
}
+2 -1
View File
@@ -702,12 +702,13 @@ static CBMFileResult *sem_get_or_extract(cbm_pipeline_ctx_t *ctx, int file_idx,
}
return NULL;
}
CBMIncludeScope include_scope = cbm_pipeline_include_scope(ctx);
CBMFileResult *r =
cbm_pipeline_test_force_extract_null(ctx, fi->language)
? NULL
: cbm_extract_file_ex_with_tests(source, source_len, fi->language, ctx->project_name,
fi->rel_path, CBM_EXTRACT_BUDGET, NULL, NULL, NULL,
NULL, ctx->test_declarations);
NULL, ctx->test_declarations, &include_scope);
cbm_free(CBM_MEM_CLASS_SEMANTIC, source);
(void)cbm_pipeline_test_extraction_ok(ctx, fi->language, r);
if (r) {
+18 -2
View File
@@ -18,6 +18,7 @@
#include "pipeline/pipeline_internal.h"
#include "pipeline/lsp_resolve.h"
#include "pipeline/pass_lsp_cross.h"
#include "pipeline/site_bind.h"
#include "graph_buffer/graph_buffer.h"
#include "foundation/log.h"
#include "foundation/compat.h"
@@ -161,6 +162,8 @@ static int resolve_usage_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *res
/* imp_keys is the IMPORTS-edge map (cbm_pxc_build_import_map). */
cbm_php_vendor_names_t php_vendor;
cbm_pipeline_php_vendor_names_build(ctx->gbuf, ctx->project_name, rel, result, &php_vendor);
cbm_site_file_t site;
cbm_pipeline_site_bind_begin(&site, ctx, ctx->gbuf, rel, lang, result);
for (int u = 0; u < result->usages.count; u++) {
CBMUsage *usage = &result->usages.items[u];
if (!usage->ref_name) {
@@ -237,11 +240,21 @@ static int resolve_usage_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *res
}
}
/* The shared cross-file layer (site_bind.h) is asked at every usage no
* engine row was found for; a proven answer is the target and main's
* name chain does not run for this site. MUST match pass_parallel.c. */
cbm_site_answer_t scoped = {.verdict = CBM_SITE_NO_ANSWER};
if (!tgt && !semantic_reference && !property_read) {
scoped = cbm_pipeline_site_bind_usage(&site, usage, src);
}
/* A syntactically explicit reference remains useful when exact semantic
* resolution is unavailable, but the textual registry fallback proves
* only value use—not an occurrence-exact callable target. Emit USAGE in
* that case; CALL_REFERENCE is reserved for the exact LSP join. */
if (!tgt) {
if (scoped.verdict == CBM_SITE_PROVEN) {
tgt = scoped.target;
} else if (!tgt) {
/* An occurrence-exact semantic record owns this reference even when
* its target is not materialized in the graph (for example, a
* Kotlin local function). Falling back by raw name here would bind
@@ -317,6 +330,7 @@ static int resolve_usage_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *res
cbm_gbuf_insert_edge(ctx->gbuf, src->id, tgt->id, edge_type, uprops);
resolved++;
}
cbm_pipeline_site_bind_end(&site, ctx->gbuf);
cbm_pipeline_lsp_reference_index_free(&reference_index);
cbm_pipeline_lsp_field_index_free(&field_index);
cbm_pipeline_php_vendor_names_free(&php_vendor);
@@ -477,11 +491,13 @@ int cbm_pipeline_pass_usages(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *fil
errors++;
continue;
}
CBMIncludeScope include_scope = cbm_pipeline_include_scope(ctx);
result = cbm_pipeline_test_force_extract_null(ctx, files[i].language)
? NULL
: cbm_extract_file_ex_with_tests(
source, source_len, files[i].language, ctx->project_name, rel,
CBM_EXTRACT_BUDGET, NULL, NULL, NULL, NULL, ctx->test_declarations);
CBM_EXTRACT_BUDGET, NULL, NULL, NULL, NULL, ctx->test_declarations,
&include_scope);
free(source);
if (!cbm_pipeline_test_extraction_ok(ctx, files[i].language, result)) {
cbm_free_result(result);
+49 -39
View File
@@ -1007,6 +1007,27 @@ bool cbm_pipeline_set_project_name(cbm_pipeline_t *p, const char *name) {
return true;
}
CBMHashTable *cbm_pipeline_indexed_files_new(const cbm_file_info_t *files, int file_count) {
CBMHashTable *set = cbm_ht_create((uint32_t)((size_t)file_count * PAIR_LEN + CBM_SZ_64));
for (int i = 0; set && i < file_count; i++) {
cbm_ht_set(set, files[i].rel_path, (void *)&files[i]);
}
return set;
}
static bool pipeline_file_is_indexed(const void *userdata, const char *rel_path) {
return userdata && cbm_ht_get((const CBMHashTable *)userdata, rel_path) != NULL;
}
CBMIncludeScope cbm_pipeline_include_scope(const cbm_pipeline_ctx_t *ctx) {
CBMIncludeScope scope = {
.root = ctx ? ctx->repo_path : NULL,
.is_indexed = pipeline_file_is_indexed,
.userdata = ctx ? ctx->indexed_files : NULL,
};
return scope;
}
void cbm_pipeline_set_lsp_surfaces(cbm_pipeline_t *p, cbm_lsp_surface_row_t *rows, int count) {
if (!p) {
cbm_store_free_lsp_surfaces(rows, count);
@@ -3106,49 +3127,30 @@ static int run_parallel_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
CBMModuleDefIndex *module_def_index =
all_defs ? cbm_pxc_build_module_def_index(all_defs, def_count) : NULL;
CBM_PROF_END_N("lsp_cross_prepare", "3_module_def_index", t_module_index, def_count);
/* Tier 2 full: pre-build per-language cross-LSP registries.
* Built ONCE here; shared READ-ONLY across all files of that language
* during resolve. Per-file work is then: parse + AST walk + O(1) lookups
* — no registry build, no Phase 1b mutations. Languages added so far:
* Go, Python, C/C++, C#, TS/JS, Java. Others (Kotlin, PHP) fall back to per-file. */
CBMCrossLspRegistries cross_registries = {0};
/* Tier 2 full: pre-build each engine's shared cross-LSP registry (the
* engine table, pass_lsp_cross.c). Built ONCE here; shared READ-ONLY
* across all files of that engine's languages during resolve. Per-file
* work is then: parse + AST walk + O(1) lookups — no registry build, no
* Phase 1b mutations. Engines without one (Kotlin, PHP) take the per-file
* path. Rust's is lazy: built on the first NULL-filter rust file (the
* amplifier files) inside cbm_parallel_resolve — repos whose rust files
* all filter to subsets never pay the build/RSS. */
CBMCrossLspRegistries cross_registries;
memset(&cross_registries, 0, sizeof(cross_registries));
cross_registries.ctx = ctx;
if (all_defs) {
/* Per-builder split of lsp_cross_prepare — attributes a slow prepare to
* ONE language instead of re-diagnosing the whole pass (the cs builder
* ONE engine instead of re-diagnosing the whole pass (the cs builder
* hid ~140 s behind the pass total, #1669 follow-up). */
struct timespec t_b;
long b_ms[6];
cbm_clock_gettime(CLOCK_MONOTONIC, &t_b);
cross_registries.go = cbm_go_build_cross_registry(&cross_lsp_arena, all_defs, def_count);
b_ms[0] = (long)elapsed_ms(t_b);
cbm_clock_gettime(CLOCK_MONOTONIC, &t_b);
cross_registries.python =
cbm_py_build_cross_registry(&cross_lsp_arena, all_defs, def_count);
b_ms[1] = (long)elapsed_ms(t_b);
cbm_clock_gettime(CLOCK_MONOTONIC, &t_b);
cross_registries.c = cbm_c_build_cross_registry(&cross_lsp_arena, all_defs, def_count);
b_ms[2] = (long)elapsed_ms(t_b);
cbm_clock_gettime(CLOCK_MONOTONIC, &t_b);
cross_registries.cs = cbm_cs_build_cross_registry(&cross_lsp_arena, all_defs, def_count);
b_ms[3] = (long)elapsed_ms(t_b);
cbm_clock_gettime(CLOCK_MONOTONIC, &t_b);
cross_registries.ts = cbm_ts_build_cross_registry(&cross_lsp_arena, all_defs, def_count);
b_ms[4] = (long)elapsed_ms(t_b);
cbm_clock_gettime(CLOCK_MONOTONIC, &t_b);
cross_registries.java =
cbm_java_build_cross_registry(&cross_lsp_arena, all_defs, def_count);
b_ms[5] = (long)elapsed_ms(t_b);
char b_buf[6][CBM_SZ_16];
const char *b_name[6] = {"go", "python", "c", "cs", "ts", "java"};
for (int bi = 0; bi < 6; bi++) {
snprintf(b_buf[bi], sizeof(b_buf[bi]), "%ld", b_ms[bi]);
for (int engine = 0; engine < cbm_pxc_engine_count(); engine++) {
struct timespec t_b;
cbm_clock_gettime(CLOCK_MONOTONIC, &t_b);
if (cbm_pxc_build_engine_registry(engine, &cross_lsp_arena, all_defs, def_count,
&cross_registries)) {
cbm_log_info("lsp_cross_prepare.builder", "engine", cbm_pxc_engine_name(engine),
"elapsed_ms", itoa_buf((int)elapsed_ms(t_b)));
}
}
cbm_log_info("lsp_cross_prepare.builders", b_name[0], b_buf[0], b_name[1], b_buf[1],
b_name[2], b_buf[2], b_name[3], b_buf[3], b_name[4], b_buf[4], b_name[5],
b_buf[5]);
/* Rust: NOT built here. The shared all_defs registry is built LAZILY on the
* first NULL-filter rust file (the amplifier files) inside cbm_parallel_resolve
* — repos whose rust files all filter to subsets never pay the build/RSS. */
}
cbm_log_info("pass.timing", "pass", "lsp_cross_prepare", "elapsed_ms",
itoa_buf((int)elapsed_ms(*t)));
@@ -4394,6 +4396,7 @@ static int cbm_pipeline_run_staged(cbm_pipeline_t *p) {
struct timespec t0;
cbm_clock_gettime(CLOCK_MONOTONIC, &t0);
cbm_path_alias_collection_t *path_aliases = NULL;
CBMHashTable *indexed_files = NULL;
cbm_file_hash_t *baseline_manifest = NULL;
int baseline_count = 0;
char **requested_excluded_dirs = NULL;
@@ -4559,11 +4562,17 @@ static int cbm_pipeline_run_staged(cbm_pipeline_t *p) {
* when no usable configs are found — non-TS projects pay nothing. */
path_aliases =
cbm_load_path_aliases_excluded(p->repo_path, p->excluded_dirs, p->excluded_count);
indexed_files = cbm_pipeline_indexed_files_new(files, file_count);
if (!indexed_files) {
rc = CBM_NOT_FOUND;
goto cleanup;
}
/* Build shared context for pass functions */
cbm_pipeline_ctx_t ctx = {
.project_name = p->project_name,
.repo_path = p->repo_path,
.indexed_files = indexed_files,
.gbuf = p->gbuf,
.registry = p->registry,
.cancelled = p->cancelled,
@@ -4609,6 +4618,7 @@ cleanup:
cbm_registry_free(p->registry);
p->registry = NULL;
cbm_path_alias_collection_free(path_aliases);
cbm_ht_free(indexed_files); /* keys borrowed from files */
if (restore_requested_discovery) {
cbm_discover_free_excluded(p->excluded_dirs, p->excluded_count);
cbm_discover_free_ignored(p->ignored_files, p->ignored_count);
+108 -14
View File
@@ -2002,8 +2002,11 @@ static int run_extract_resolve(cbm_pipeline_ctx_t *ctx, cbm_file_info_t *changed
int all_def_count = 0;
char **def_modules = NULL;
CBMModuleDefIndex *module_def_index = NULL;
CBMCrossLspRegistries cross_registries = {0};
CBMCrossLspRegistries *registries_arg = NULL;
/* Every route hands the walks the context; only a closure above the
* floor builds shared registries into it. */
CBMCrossLspRegistries cross_registries;
memset(&cross_registries, 0, sizeof(cross_registries));
cross_registries.ctx = ctx;
CBMArena legacy_cross_arena;
bool legacy_cross_arena_live = false;
bool owner_walk_required = atomic_load(&ctx->test_definition_owners_seen) != 0;
@@ -2074,16 +2077,8 @@ static int run_extract_resolve(cbm_pipeline_ctx_t *ctx, cbm_file_info_t *changed
* for languages without a shared registry, so convergence
* is unaffected; only the amortization strategy changes. */
if (ci > CLOSURE_BUDGET_FLOOR_FILES) {
CBMArena *xa = &closure->arena;
cross_registries.go = cbm_go_build_cross_registry(xa, all_defs, all_def_count);
cross_registries.python =
cbm_py_build_cross_registry(xa, all_defs, all_def_count);
cross_registries.c = cbm_c_build_cross_registry(xa, all_defs, all_def_count);
cross_registries.cs = cbm_cs_build_cross_registry(xa, all_defs, all_def_count);
cross_registries.ts = cbm_ts_build_cross_registry(xa, all_defs, all_def_count);
cross_registries.java =
cbm_java_build_cross_registry(xa, all_defs, all_def_count);
registries_arg = &cross_registries;
cbm_pxc_build_cross_registries(&closure->arena, all_defs, all_def_count,
&cross_registries);
}
} else {
all_def_count = 0;
@@ -2122,7 +2117,7 @@ static int run_extract_resolve(cbm_pipeline_ctx_t *ctx, cbm_file_info_t *changed
cbm_doclinks_begin(ctx, changed_files, ci, cache);
rc = cbm_parallel_resolve(
ctx, changed_files, ci, cache, &shared_ids, worker_count, all_defs, all_def_count,
closure ? closure->def_modules : def_modules, module_def_index, registries_arg);
closure ? closure->def_modules : def_modules, module_def_index, &cross_registries);
cbm_doclinks_end(ctx);
cross_resolve_cleanup:
@@ -2388,6 +2383,7 @@ static void *rehydrate_worker(void *arg) {
* correctness stays trivially reasoned. Returns heap rows via out. */
static int closure_probe_surfaces(cbm_pipeline_t *p, const char *project,
cbm_file_info_t *probe_files, int probe_count,
const CBMHashTable *indexed_files,
cbm_lsp_surface_row_t **out_rows, int *out_count) {
*out_rows = NULL;
*out_count = 0;
@@ -2403,6 +2399,7 @@ static int closure_probe_surfaces(cbm_pipeline_t *p, const char *project,
cbm_pipeline_ctx_t probe_ctx = {
.project_name = project,
.repo_path = cbm_pipeline_repo_path(p),
.indexed_files = indexed_files,
.gbuf = probe_gbuf,
.registry = NULL,
.cancelled = cbm_pipeline_cancelled_ptr(p),
@@ -3046,6 +3043,85 @@ static bool closure_metadata_current(bool metadata_current, const cbm_file_hash_
return current && old_count == new_count;
}
/* Does `list` (one repository path per line, as lsp_surface.included_files
* stores it) name a file of `delta`? Every listed file must have been tracked
* by the previous generation: a file it did not hash cannot be proven
* unchanged, which *untracked reports. */
static bool closure_list_names_delta(const char *list, const CBMHashTable *delta,
const CBMHashTable *stored_by_path, bool *untracked) {
bool hit = false;
char name[CBM_SZ_4K];
for (const char *at = list; at && *at;) {
const char *end = strchr(at, '\n');
size_t n = end ? (size_t)(end - at) : strlen(at);
if (n > 0) {
if (n >= sizeof(name)) {
*untracked = true;
return hit;
}
memcpy(name, at, n);
name[n] = '\0';
if (!cbm_ht_get(stored_by_path, name)) {
*untracked = true;
}
if (cbm_ht_get(delta, name)) {
hit = true;
}
}
at = end ? end + 1 : at + n;
}
return hit;
}
/* The includers of a changed or deleted file (#2993). A C-family file's
* preprocessed pass reads the repository files its #include lines name, so
* its graph changes with their bytes even when its own bytes do not. Every
* stored surface row lists the files its file's lookup read, transitively
* (the headers a header includes are on the includer's own list), so one pass
* over the rows finds every includer. Each joins the delta as a changed file
* and goes through the same surface comparison as the others. `paths` holds
* the changed files, then the deleted ones; the includers are inserted after
* the changed files. Linear in the total length of the lists. Returns a
* decline reason, or NULL. */
static const char *closure_add_includers(const cbm_lsp_surface_row_t *rows, int row_count,
const CBMHashTable *stored_by_path,
const CBMHashTable *files_by_path, char **paths,
int *n_changed, int n_deleted) {
int changed = *n_changed;
CBMHashTable *delta =
cbm_ht_create((uint32_t)((size_t)(changed + n_deleted) * PAIR_LEN + CBM_SZ_64));
char **found = (char **)cbm_alloc(CBM_MEM_CLASS_OTHER,
(size_t)(row_count > 0 ? row_count : 1) * sizeof(char *));
const char *why = !delta || !found ? "alloc" : NULL;
for (int i = 0; !why && i < changed + n_deleted; i++) {
cbm_ht_set(delta, paths[i], paths[i]);
}
int found_count = 0;
for (int i = 0; !why && i < row_count; i++) {
bool untracked = false;
bool hit =
closure_list_names_delta(rows[i].included_files, delta, stored_by_path, &untracked);
if (untracked) {
why = "include_untracked";
} else if (hit && !cbm_ht_get(delta, rows[i].rel_path)) {
if (!cbm_ht_get(files_by_path, rows[i].rel_path)) {
why = "includer_not_discovered";
} else {
found[found_count++] = (char *)rows[i].rel_path;
}
}
}
if (!why && found_count > 0) {
memmove(paths + changed + found_count, paths + changed, (size_t)n_deleted * sizeof(char *));
memcpy(paths + changed, found, (size_t)found_count * sizeof(char *));
*n_changed = changed + found_count;
cbm_log_info("incremental.closure_includers", "includers", itoa_buf(found_count));
}
cbm_free(CBM_MEM_CLASS_OTHER, found);
cbm_ht_free(delta); /* keys borrowed */
return why;
}
/* Decide whether this manifest delta is closure-repairable, and if so build
* the plan. Returns 1 with *plan filled, or 0 (decline → FULL) with the
* reason logged. Every early exit declines; nothing here mutates state. */
@@ -3229,6 +3305,12 @@ static int closure_try_plan(cbm_pipeline_t *p, cbm_store_t *store, const char *p
for (int i = 0; i < plan->stored_count; i++) {
cbm_ht_set(rows_by_path, plan->stored_rows[i].rel_path, &plan->stored_rows[i]);
}
decline = closure_add_includers(plan->stored_rows, plan->stored_count, stored_by_path,
files_by_path, changed_paths, &n_changed, n_deleted);
if (decline) {
cbm_ht_free(rows_by_path);
goto done;
}
probe_files =
(cbm_file_info_t *)calloc((size_t)(n_changed ? n_changed : 1), sizeof(cbm_file_info_t));
@@ -3247,7 +3329,7 @@ static int closure_try_plan(cbm_pipeline_t *p, cbm_store_t *store, const char *p
probe_files[i] = *info;
}
int probe_rc = n_changed > 0 ? closure_probe_surfaces(p, project, probe_files, n_changed,
&probe_rows, &probe_count)
files_by_path, &probe_rows, &probe_count)
: 0;
if (probe_rc != 0) {
cbm_ht_free(rows_by_path);
@@ -3619,6 +3701,7 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
cbm_gbuf_t *gbuf = NULL;
cbm_registry_t *registry = NULL;
cbm_path_alias_collection_t *path_aliases = NULL;
CBMHashTable *indexed_files = NULL;
CBMHashTable *stale_surface_set = NULL;
closure_resolve_t cr = {0};
bool cr_arena_live = false;
@@ -3831,6 +3914,11 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
cbm_log_error("delta.err", "phase", "doc_link_scopes");
goto out;
}
indexed_files = cbm_pipeline_indexed_files_new(files, file_count);
if (!indexed_files) {
cbm_log_error("delta.err", "phase", "indexed_files");
goto out;
}
/* The purge already removed the closure and deleted files, so every File
* row left in the stage is a file the passes will not see. A failed read
* is a delta-route failure: the maps would silently diverge from a full
@@ -3844,6 +3932,7 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
cbm_pipeline_ctx_t ctx = {
.project_name = project,
.repo_path = cbm_pipeline_repo_path(p),
.indexed_files = indexed_files,
.gbuf = gbuf,
.registry = registry,
.cancelled = cbm_pipeline_cancelled_ptr(p),
@@ -4186,6 +4275,7 @@ out:
cbm_registry_free(registry);
}
cbm_path_alias_collection_free(path_aliases);
cbm_ht_free(indexed_files); /* keys borrowed from files */
if (gbuf) {
cbm_gbuf_free(gbuf);
}
@@ -4599,10 +4689,12 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
cbm_path_alias_collection_t *path_aliases =
cbm_load_path_aliases_excluded(cbm_pipeline_repo_path(p), excluded_dirs, excluded_count);
CBMHashTable *indexed_files = cbm_pipeline_indexed_files_new(files, file_count);
cbm_pipeline_ctx_t ctx = {
.project_name = project,
.repo_path = cbm_pipeline_repo_path(p),
.indexed_files = indexed_files,
.gbuf = existing,
.registry = registry,
.cancelled = cbm_pipeline_cancelled_ptr(p),
@@ -4690,6 +4782,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
free(changed_files);
cbm_registry_free(registry);
cbm_path_alias_collection_free(path_aliases);
cbm_ht_free(indexed_files); /* keys borrowed from files */
incr_free_edge_capture(&edge_cap);
cbm_store_free_coverage(old_cov, old_cov_count);
free_mode_skipped(mode_skipped, mode_skipped_count);
@@ -4789,6 +4882,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
free(changed_files);
cbm_registry_free(registry);
cbm_path_alias_collection_free(path_aliases);
cbm_ht_free(indexed_files); /* keys borrowed from files */
/* Re-link inbound cross-file edges that the purge orphaned. Runs after
* re-resolution AND post-passes so the freshly re-created target nodes
+11
View File
@@ -458,6 +458,10 @@ typedef struct cbm_tf_release {
typedef struct {
const char *project_name; /* borrowed from pipeline */
const char *repo_path; /* borrowed from pipeline */
/* The rel_path of every file of the run's whole discovery (keys borrowed
* from its file list), also when a pass is handed only some of them: the
* files a C-family #include may resolve to (#2993). NULL: none resolves. */
const CBMHashTable *indexed_files;
/* An incremental run: the unchanged files whose earlier run declared
* image builds (docker build ...), read again by the shell pass, as
* those edges are recomputed whole. Borrowed; NULL in a full run. */
@@ -577,6 +581,13 @@ typedef struct {
int tf_release_cap;
} cbm_pipeline_ctx_t;
/* The set for cbm_pipeline_ctx_t.indexed_files: every rel_path of `files`
* (keys borrowed; free with cbm_ht_free). NULL on allocation failure. */
CBMHashTable *cbm_pipeline_indexed_files_new(const cbm_file_info_t *files, int file_count);
/* The #include scope of a pass's C-family extraction: the run's repository
* root and its indexed files. */
CBMIncludeScope cbm_pipeline_include_scope(const cbm_pipeline_ctx_t *ctx);
/* Origin-aware LSP returns only success/failure. Never retain partial output
* when its configured owner proof cannot be applied to current source. */
static inline void cbm_pipeline_test_owner_error(CBMFileResult *result) {
+2 -3
View File
@@ -27,6 +27,7 @@ enum { REG_MAX_CANDIDATES = 256 };
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
#include "cbm.h" /* cbm_label_is_relation — the resolve-time relation veto */
#include "discover/discover.h" /* cbm_language_is_build_config */
#include "foundation/compat.h" /* CBM_TLS */
#include "foundation/hash_table.h"
#include "foundation/arena.h"
@@ -1434,9 +1435,7 @@ static const char *path_basename(const char *path) {
* file, so a bare-name bind into another language is always a collision
* (2026-09-16 probe: kernel Makefile targets bound to `sk_psock.eval`). */
static bool build_config_language(CBMLanguage lang) {
return lang == CBM_LANG_MAKEFILE || lang == CBM_LANG_CMAKE || lang == CBM_LANG_YAML ||
lang == CBM_LANG_TOML || lang == CBM_LANG_JSON || lang == CBM_LANG_INI ||
lang == CBM_LANG_DOCKERFILE;
return cbm_language_is_build_config(lang); /* the language registry's list */
}
bool cbm_suppress_cross_language_suffix_match(CBMLanguage caller_lang, const char *target_file_path,
+58
View File
@@ -0,0 +1,58 @@
/*
* site_bind.c — answers of the shared cross-file layer at call and usage
* sites. See site_bind.h for where the resolvers ask and how they apply an
* answer.
*
* No language has a row yet, so begin marks no file active, every site gets
* NO_ANSWER and the end step draws nothing: the hook costs a few calls per
* file and one call per site.
*/
#include "pipeline/site_bind.h"
#include "cbm.h"
#include "graph_buffer/graph_buffer.h"
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
#include <stdbool.h>
#include <stddef.h>
void cbm_pipeline_site_bind_begin(cbm_site_file_t *sf, const cbm_pipeline_ctx_t *ctx,
const cbm_gbuf_t *graph, const char *rel, CBMLanguage lang,
const CBMFileResult *result) {
if (!sf) {
return;
}
*sf = (cbm_site_file_t){
.ctx = ctx, .graph = graph, .result = result, .rel = rel, .lang = lang, .active = false};
}
/* The answer at every site of a file no row covers. */
static cbm_site_answer_t sb_no_answer(void) {
cbm_site_answer_t none = {.verdict = CBM_SITE_NO_ANSWER, .res = {0}, .target = NULL};
return none;
}
cbm_site_answer_t cbm_pipeline_site_bind_call(cbm_site_file_t *sf, const CBMCall *call,
const cbm_gbuf_node_t *source) {
/* No row exists, so no file is active. */
(void)sf;
(void)call;
(void)source;
return sb_no_answer();
}
cbm_site_answer_t cbm_pipeline_site_bind_usage(cbm_site_file_t *sf, const CBMUsage *usage,
const cbm_gbuf_node_t *source) {
/* No row exists, so no file is active. */
(void)sf;
(void)usage;
(void)source;
return sb_no_answer();
}
void cbm_pipeline_site_bind_end(cbm_site_file_t *sf, cbm_gbuf_t *edges) {
/* No POSSIBLE answer was given, so nothing is left to draw. */
(void)sf;
(void)edges;
}
+88
View File
@@ -0,0 +1,88 @@
#ifndef CBM_SITE_BIND_H
#define CBM_SITE_BIND_H
/*
* site_bind.h — the one place where the shared cross-file layer answers a
* call or usage site.
*
* Both call resolvers (pass_calls.c, pass_parallel.c) and both usage
* resolvers (pass_usages.c, pass_parallel.c) ask here at the same sites and
* apply the answer the same way, so the two routes cannot drift:
*
* asked a call site that no engine row matched (no CBMResolvedCall)
* and that does not require one, before main's service
* classification; a usage site that no engine row was found
* for, after the C member join and before the PHP vendor check
* and main's name chain.
* PROVEN replaces main's name chain at the site. A call keeps every
* later step of main (service and route classification, spawn,
* field policy, guards) on the answer; a usage takes the target.
* POSSIBLE main's chain decides the site as before. The layer may draw
* its own edge for it when the file is done
* (cbm_pipeline_site_bind_end), and only where main drew no
* edge of that type between the same two nodes.
* NO_ANSWER main's chain, unchanged.
*
* Rules and their rows live in site_bind.c; a resolver is never edited for
* a new rule. No language has a row yet: every file is inactive, every site
* gets NO_ANSWER and the end step draws nothing, so the graph is main's on
* both routes.
*
* Threads: one cbm_site_file_t per file being resolved, on the stack of the
* worker resolving it. Whatever run-wide input a row reads is built and
* sealed before any resolver starts.
*/
#include "cbm.h"
#include "graph_buffer/graph_buffer.h"
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
#include <stdbool.h>
typedef enum {
CBM_SITE_NO_ANSWER = 0, /* main's chain decides, unchanged */
CBM_SITE_PROVEN, /* replaces main's chain at this site */
CBM_SITE_POSSIBLE, /* main's chain decides; an own edge may follow at the file's end */
} cbm_site_verdict_t;
typedef struct {
cbm_site_verdict_t verdict;
/* PROVEN only: the answer in the shape main's chain returns
* (qualified_name is target's QN, candidate_count 1), and its node,
* which the run's graph is known to hold. */
cbm_resolution_t res;
const cbm_gbuf_node_t *target;
} cbm_site_answer_t;
/* One file's state, on the resolver's stack between begin and end. */
typedef struct {
const cbm_pipeline_ctx_t *ctx;
const cbm_gbuf_t *graph; /* the run's nodes, read-only while resolving */
const CBMFileResult *result;
const char *rel;
CBMLanguage lang;
bool active; /* false: no row covers this file, every site is NO_ANSWER */
} cbm_site_file_t;
/* Start one file: before its first site is asked. */
void cbm_pipeline_site_bind_begin(cbm_site_file_t *sf, const cbm_pipeline_ctx_t *ctx,
const cbm_gbuf_t *graph, const char *rel, CBMLanguage lang,
const CBMFileResult *result);
/* The answer for one call of sf->result (an element of result->calls),
* made in the function or file node `source`. */
cbm_site_answer_t cbm_pipeline_site_bind_call(cbm_site_file_t *sf, const CBMCall *call,
const cbm_gbuf_node_t *source);
/* The answer for one usage of sf->result (an element of result->usages),
* made in `source`. */
cbm_site_answer_t cbm_pipeline_site_bind_usage(cbm_site_file_t *sf, const CBMUsage *usage,
const cbm_gbuf_node_t *source);
/* End one file: after its last site, with the buffer its edges went to
* (the run's graph on the sequential route, the worker's edge buffer on the
* parallel one). Draws what POSSIBLE answers leave to this step. */
void cbm_pipeline_site_bind_end(cbm_site_file_t *sf, cbm_gbuf_t *edges);
#endif /* CBM_SITE_BIND_H */
+313 -6
View File
@@ -28,22 +28,39 @@ static const char *const go_labels[] = {"Struct", "Interface", "Type", "Class",
/* Python: what py_register_lsp_defs registers as a type. */
static const char *const py_labels[] = {"Class", "Interface", "Protocol", "Type", NULL};
/* One row per language whose engine reads identities. A row states every
* field; a further language plugs in by appending its row. */
static const cbm_type_scope_lang_t type_scope_langs[] = {
{.lang = CBM_LANG_GO,
.scope_source = CBM_TYPE_SCOPE_MODULE_IMPORTS,
.quoted_names = false,
.colon_paths = false,
.labels = go_labels,
.engine_qualifies_own_module = true,
.imports_bind_modules = true,
.lexical_scopes = false,
.open_imports = false, /* a dot import binds "."; no spelling names it */
.implicit_external = NULL,
.nested_types = false,
.return_types = true,
.param_types = true,
.field_types = true,
.embedded_types = true},
{.lang = CBM_LANG_PYTHON,
.scope_source = CBM_TYPE_SCOPE_MODULE_IMPORTS,
.quoted_names = true,
.colon_paths = false,
.labels = py_labels,
.engine_qualifies_own_module = false,
.imports_bind_modules = false,
.lexical_scopes = true,
.open_imports = false, /* a star import closes the file (module facts) */
.implicit_external = NULL,
.nested_types = false, /* a class body's names shadow: no identity */
.return_types = true,
.param_types = true},
.param_types = true,
.field_types = false, /* folded as resolved QNs (pass_lsp_cross.c) */
.embedded_types = false /* bases arrive as resolved QNs */},
};
const cbm_type_scope_lang_t *cbm_type_scope_lang(CBMLanguage lang) {
@@ -112,7 +129,21 @@ struct cbm_type_scope_file {
bool closed; /* the module's bindings cannot be listed: no identity */
uintptr_t serial; /* the def being attached, from 1 */
const char *enclosing_class; /* the def's immediately enclosing class, or NULL */
CBMArena keys; /* memo keys and candidate strings */
/* open_imports only: the packages the file's open imports name, each once,
* in import-map order. */
const char **open;
int open_count;
/* nested_types and lexical nesting: "<enclosing QN>\x1f<name>" -> the
* answer through that enclosing scope and its outer ones (a QN, or
* TYPE_SCOPE_MISS). */
CBMHashTable *nest_memo;
const char *nest_start; /* the def being attached: its innermost enclosing scope */
/* the calling-file table (cbm_type_scope_file_imports), built once */
const char **fi_locals;
const char **fi_qns;
int fi_count;
bool fi_built;
CBMArena keys; /* memo keys and candidate strings */
};
static bool type_scope_function_label(const char *label) {
@@ -336,8 +367,14 @@ cbm_type_scope_file_t *cbm_type_scope_file_begin(const cbm_type_scope_lang_t *la
f->imports = cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, (uint32_t)(scope->imp_count + 1));
f->memo = cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, 32);
f->seen = cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, 32);
bool nests = lang->nested_types || lang->scope_source == CBM_TYPE_SCOPE_LEXICAL_NESTING;
f->nest_memo = nests ? cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, 32) : NULL;
cbm_arena_init_lazy(&f->keys, CBM_SZ_4K);
if (!f->imports || !f->memo || !f->seen ||
if (lang->open_imports && scope->imp_count > 0) {
f->open = cbm_arena_alloc(&f->keys, (size_t)scope->imp_count * sizeof(*f->open));
}
if (!f->imports || !f->memo || !f->seen || (nests && !f->nest_memo) ||
(lang->open_imports && scope->imp_count > 0 && !f->open) ||
(lang->lexical_scopes && !type_scope_index_file_defs(f))) {
cbm_type_scope_file_end(f);
return NULL;
@@ -349,6 +386,15 @@ cbm_type_scope_file_t *cbm_type_scope_file_begin(const cbm_type_scope_lang_t *la
continue;
}
TYPE_SCOPE_WORK(1);
if (lang->open_imports && strcmp(key, CBM_TYPE_SCOPE_OPEN_KEY) == 0) {
/* An open import: its package, once (one probe of the memo). */
char *dedupe = cbm_arena_sprintf(&f->keys, "\x1f%s", val);
if (dedupe && !cbm_ht_get(f->memo, dedupe)) {
cbm_ht_set(f->memo, dedupe, (void *)val);
f->open[f->open_count++] = val;
}
continue;
}
void *prev = cbm_ht_get(f->imports, key);
if (!prev) {
cbm_ht_set(f->imports, key, (void *)val);
@@ -373,6 +419,7 @@ void cbm_type_scope_file_end(cbm_type_scope_file_t *file) {
cbm_ht_free(file->tparams);
cbm_ht_free(file->nesting);
cbm_ht_free(file->bindings);
cbm_ht_free(file->nest_memo);
cbm_arena_destroy(&file->keys);
cbm_free(CBM_MEM_CLASS_EXTRACT, file);
}
@@ -447,6 +494,116 @@ static const char *type_scope_via_import(cbm_type_scope_file_t *f, const char *d
return direct ? direct : member;
}
/* open_imports / implicit_external: the one target `<package>.<dotted>` names
* across the file's open packages, for a name the own module and the named
* imports leave unbound. A head the own module declares (any label) shadows
* the open imports; a head the implicit external scope declares, and two
* different targets, are no answer. */
static const char *type_scope_via_open(cbm_type_scope_file_t *f, const char *dotted,
size_t head_len) {
const char *mod = f->scope.module_qn;
int scope_len = (int)f->scope_len;
const char *head_qn =
cbm_arena_sprintf(&f->keys, "%.*s.%.*s", scope_len, mod, (int)head_len, dotted);
TYPE_SCOPE_WORK(1);
if (!head_qn || cbm_registry_exists(f->scope.reg, head_qn)) {
return NULL;
}
if (f->lang->implicit_external && f->lang->implicit_external(dotted, head_len)) {
return NULL;
}
const char *found = NULL;
for (int i = 0; i < f->open_count; i++) {
const char *qn = cbm_arena_sprintf(&f->keys, "%s.%s", f->open[i], dotted);
const char *hit = qn ? type_scope_target(f, qn) : NULL;
if (hit && found && strcmp(hit, found) != 0) {
return NULL;
}
found = hit ? hit : found;
}
return found;
}
/* CBM_TYPE_SCOPE_INCLUDE_CLOSURE: the head through the caller's include
* closure, the rest named inside it. */
static const char *type_scope_via_visible(cbm_type_scope_file_t *f, const char *dotted,
size_t head_len) {
if (!f->scope.visible || head_len == 0) {
return NULL;
}
TYPE_SCOPE_WORK(1);
const char *head = f->scope.visible(f->scope.visible_ud, dotted, head_len);
if (!head) {
return NULL;
}
if (dotted[head_len] != '.') {
return type_scope_target(f, head);
}
const char *qn = cbm_arena_sprintf(&f->keys, "%s.%s", head, dotted + head_len + 1);
return qn ? type_scope_target(f, qn) : NULL;
}
/* Nested scopes (nested_types, lexical nesting): the innermost scope, from
* `start` outward through its QN prefixes no shorter than `min_len` bytes,
* that declares `head` ("<scope>.<head>" names a project definition of any
* label), or NULL. Memoized per (scope, head) for every scope the walk passes,
* so a name written at nesting depth d costs at most d probes once per
* file. */
static const char *type_scope_declaring_scope(cbm_type_scope_file_t *f, const char *start,
size_t min_len, const char *head, size_t head_len) {
const char **passed = NULL; /* memo keys walked through, not yet answered */
int count = 0;
int cap = 0;
const char *answer = NULL;
size_t cur_len = start ? strlen(start) : 0;
while (cur_len >= min_len && cur_len > 0) {
char *key =
cbm_arena_sprintf(&f->keys, "%.*s\x1f%.*s", (int)cur_len, start, (int)head_len, head);
TYPE_SCOPE_WORK(cur_len + head_len + 1);
if (!key) {
break;
}
void *known = cbm_ht_get(f->nest_memo, key);
if (known) {
answer = known == TYPE_SCOPE_MISS ? NULL : (const char *)known;
break;
}
char *qn =
cbm_arena_sprintf(&f->keys, "%.*s.%.*s", (int)cur_len, start, (int)head_len, head);
if (qn && cbm_registry_exists(f->scope.reg, qn)) {
answer = cbm_arena_strndup(&f->keys, start, cur_len);
cbm_ht_set(f->nest_memo, key, answer ? (void *)answer : TYPE_SCOPE_MISS);
break;
}
if (count == cap) {
int grown_cap = cap ? cap * 2 : 8;
const char **grown =
(const char **)cbm_arena_alloc(&f->keys, (size_t)grown_cap * sizeof(*grown));
if (!grown) {
break;
}
if (count) {
memcpy(grown, passed, (size_t)count * sizeof(*grown));
}
passed = grown;
cap = grown_cap;
}
passed[count++] = key;
const char *dot = NULL;
for (size_t i = cur_len; i > 0; i--) {
if (start[i - 1] == '.') {
dot = start + i - 1;
break;
}
}
cur_len = dot ? (size_t)(dot - start) : 0;
}
for (int i = 0; i < count; i++) {
cbm_ht_set(f->nest_memo, passed[i], answer ? (void *)answer : TYPE_SCOPE_MISS);
}
return answer;
}
/* The memo / seen key of a spelling: the spelling itself, NUL-terminated in a
* caller buffer (a name too long for it has no identity). */
static bool type_scope_key(const char *spelling, size_t len, char *buf, size_t bufsz) {
@@ -463,6 +620,34 @@ static size_t type_scope_head_len(const char *spelling, size_t len) {
return dot ? (size_t)(dot - spelling) : len;
}
/* The spelling with its parts joined by `.` (colon_paths: `a::B` is
* `a.B`), NUL-terminated in the file's arena; *out_len its length. */
static const char *type_scope_dotted(cbm_type_scope_file_t *f, const char *spelling, size_t len,
size_t *out_len) {
char *out = cbm_arena_strndup(&f->keys, spelling, len);
size_t n = len;
if (out && f->lang->colon_paths) {
size_t at = 0;
for (size_t i = 0; i < len; i++) {
if (spelling[i] == ':' && i + 1 < len && spelling[i + 1] == ':') {
out[at++] = '.';
i++;
} else {
out[at++] = spelling[i];
}
}
out[at] = '\0';
n = at;
}
*out_len = n;
return out;
}
/* Lexical nesting resolves in the definition's scopes only (the attach). */
static bool type_scope_needs_def(const cbm_type_scope_lang_t *lang) {
return lang->scope_source == CBM_TYPE_SCOPE_LEXICAL_NESTING;
}
const char *cbm_type_scope_resolve(cbm_type_scope_file_t *f, const char *spelling, size_t len) {
char probe[CBM_SZ_512];
if (!f || !spelling || !type_scope_key(spelling, len, probe, sizeof(probe))) {
@@ -478,8 +663,19 @@ const char *cbm_type_scope_resolve(cbm_type_scope_file_t *f, const char *spellin
return NULL;
}
const char *result = NULL;
const char *dotted = key; /* `T` or `pkg.T`, NUL-terminated */
size_t head_len = type_scope_head_len(dotted, len);
size_t dotted_len = len;
/* `T` or `pkg.T`, NUL-terminated */
const char *dotted = f->lang->colon_paths ? type_scope_dotted(f, key, len, &dotted_len) : key;
if (!dotted || type_scope_needs_def(f->lang)) {
cbm_ht_set(f->memo, key, TYPE_SCOPE_MISS);
return NULL;
}
size_t head_len = type_scope_head_len(dotted, dotted_len);
if (f->lang->scope_source == CBM_TYPE_SCOPE_INCLUDE_CLOSURE) {
result = type_scope_via_visible(f, dotted, head_len);
cbm_ht_set(f->memo, key, result ? (void *)result : TYPE_SCOPE_MISS);
return result;
}
if (f->bindings) {
/* A head the module binds more than once has no one answer. */
char head[CBM_SZ_256];
@@ -508,6 +704,9 @@ const char *cbm_type_scope_resolve(cbm_type_scope_file_t *f, const char *spellin
result = own_binds && !same ? NULL : via_import;
} else if (!bound) {
result = own;
if (!result && (f->open_count > 0 || f->lang->implicit_external)) {
result = type_scope_via_open(f, dotted, head_len);
}
}
cbm_ht_set(f->memo, key, result ? (void *)result : TYPE_SCOPE_MISS);
return result;
@@ -654,6 +853,9 @@ static void type_scope_names(const cbm_type_scope_lang_t *lang, const char *text
for (;;) {
if (i + 1 < n && text[i] == '.' && type_scope_ident_start((unsigned char)text[i + 1])) {
i++;
} else if (lang->colon_paths && i + 2 < n && text[i] == ':' && text[i + 1] == ':' &&
type_scope_ident_start((unsigned char)text[i + 2])) {
i += 2;
} else {
break;
}
@@ -661,6 +863,9 @@ static void type_scope_names(const cbm_type_scope_lang_t *lang, const char *text
i++;
}
}
if (lang->colon_paths && start >= 2 && text[start - 1] == ':' && text[start - 2] == ':') {
start -= 2; /* `::T`, the global scope's: kept whole, it has no identity */
}
bool member = start > 0 && text[start - 1] == '.' && (start < 2 || text[start - 2] != '.');
size_t after = i;
while (after < n && text[after] == ' ') {
@@ -675,6 +880,17 @@ static void type_scope_names(const cbm_type_scope_lang_t *lang, const char *text
/* ── Attaching identities to a definition ─────────────────────────── */
/* True when `label` is one the row's identities may name (a type the engine
* registers). */
static bool type_scope_label_allowed(const cbm_type_scope_lang_t *lang, const char *label) {
for (const char *const *l = lang->labels; label && l && *l; l++) {
if (strcmp(*l, label) == 0) {
return true;
}
}
return false;
}
typedef struct {
cbm_type_scope_file_t *file;
char *buf; /* "spelling=QN|..." being built (heap) */
@@ -722,6 +938,31 @@ static bool type_scope_append(type_scope_build_t *b, const char *s, size_t n) {
return true;
}
/* nested_types / lexical nesting: the answer through the definition's
* enclosing scopes (from file->nest_start outward). *decided: an enclosing
* scope declares the head, so its answer (possibly none) is final; a
* lexical-nesting row resolves through nested scopes only. */
static const char *type_scope_nested(cbm_type_scope_file_t *f, const char *name, size_t len,
bool *decided) {
bool lexical = type_scope_needs_def(f->lang);
*decided = lexical;
size_t dotted_len = len;
const char *dotted = type_scope_dotted(f, name, len, &dotted_len);
size_t head_len = dotted ? type_scope_head_len(dotted, dotted_len) : 0;
if (!dotted || head_len == 0) {
*decided = true; /* `::T`, or no memory: no identity */
return NULL;
}
size_t min_len = lexical ? f->scope_len : f->scope_len + 1;
const char *scope = type_scope_declaring_scope(f, f->nest_start, min_len, dotted, head_len);
if (!scope) {
return NULL;
}
*decided = true;
const char *qn = cbm_arena_sprintf(&f->keys, "%s.%s", scope, dotted);
return qn ? type_scope_target(f, qn) : NULL;
}
static void type_scope_on_name(const char *name, size_t len, void *ud) {
type_scope_build_t *b = (type_scope_build_t *)ud;
if (b->failed || type_scope_seen(b->file, name, len)) {
@@ -730,7 +971,11 @@ static void type_scope_on_name(const char *name, size_t len, void *ud) {
if (b->file->lang->lexical_scopes && type_scope_shadowed(b->file, name, len)) {
return;
}
const char *qn = cbm_type_scope_resolve(b->file, name, len);
bool decided = false;
const char *qn = b->file->nest_memo ? type_scope_nested(b->file, name, len, &decided) : NULL;
if (!decided) {
qn = cbm_type_scope_resolve(b->file, name, len);
}
if (!qn) {
return;
}
@@ -785,6 +1030,18 @@ void cbm_type_scope_attach(cbm_type_scope_file_t *file, CBMArena *arena, CBMLSPD
const cbm_type_scope_lang_t *lang = file->lang;
file->serial++;
file->enclosing_class = NULL;
file->nest_start = NULL;
if (file->nest_memo && def->qualified_name) {
/* A type's own texts are written inside it (nested_types); everything
* else, and every lexical-nesting def, inside its parent. */
bool inside_self =
!type_scope_needs_def(lang) && type_scope_label_allowed(lang, def->label);
const char *dot = strrchr(def->qualified_name, '.');
file->nest_start = inside_self ? def->qualified_name
: dot ? cbm_arena_strndup(&file->keys, def->qualified_name,
(size_t)(dot - def->qualified_name))
: NULL;
}
if (lang->lexical_scopes) {
/* A module whose bindings cannot be listed (a star import, an effect
* binding unknown names), and a def inside a function (it sees locals
@@ -819,3 +1076,53 @@ void cbm_type_scope_attach(cbm_type_scope_file_t *file, CBMArena *arena, CBMLSPD
}
cbm_free(CBM_MEM_CLASS_EXTRACT, b.buf);
}
/* ── The calling file's own identities ───────────────────────────── */
int cbm_type_scope_file_imports(cbm_type_scope_file_t *file, const char *const **out_locals,
const char *const **out_qns) {
if (out_locals) {
*out_locals = NULL;
}
if (out_qns) {
*out_qns = NULL;
}
if (!file) {
return 0;
}
if (!file->fi_built) {
file->fi_built = true;
/* Only an import-map scope has named imports to verify, and a module
* whose bindings cannot be listed has none. */
int n = file->lang->scope_source == CBM_TYPE_SCOPE_MODULE_IMPORTS && !file->closed
? file->scope.imp_count
: 0;
CBMHashTable *done =
n > 0 ? cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, (uint32_t)(n + 1)) : NULL;
file->fi_locals = done ? cbm_arena_alloc(&file->keys, (size_t)n * sizeof(char *)) : NULL;
file->fi_qns = done ? cbm_arena_alloc(&file->keys, (size_t)n * sizeof(char *)) : NULL;
for (int i = 0; file->fi_locals && file->fi_qns && i < n; i++) {
const char *local = file->scope.imp_keys ? file->scope.imp_keys[i] : NULL;
TYPE_SCOPE_WORK(1);
if (!local || !local[0] || cbm_ht_get(done, local) ||
(file->lang->open_imports && strcmp(local, CBM_TYPE_SCOPE_OPEN_KEY) == 0)) {
continue;
}
cbm_ht_set(done, local, (void *)local);
const char *qn = cbm_type_scope_resolve(file, local, strlen(local));
if (qn) {
file->fi_locals[file->fi_count] = local;
file->fi_qns[file->fi_count] = qn;
file->fi_count++;
}
}
cbm_ht_free(done); /* NULL-safe */
}
if (out_locals) {
*out_locals = (const char *const *)file->fi_locals;
}
if (out_qns) {
*out_qns = (const char *const *)file->fi_qns;
}
return file->fi_count;
}
+59 -1
View File
@@ -47,12 +47,34 @@
#include <stdbool.h>
#include <stddef.h>
/* Where the declarations a spelling can name come from, beyond the
* definition's own enclosing scopes. */
typedef enum {
/* The file's own module, then what its imports bind (the import map). */
CBM_TYPE_SCOPE_MODULE_IMPORTS = 0,
/* The definitions visible through the file's include closure (C, C++):
* the caller's cbm_type_scope_t.visible answers a name; without it no
* name has an identity. */
CBM_TYPE_SCOPE_INCLUDE_CLOSURE,
/* Lexical nesting (Ruby constants): the namespaces of this file that
* enclose the definition, innermost first; a name none of them declares
* has no identity. */
CBM_TYPE_SCOPE_LEXICAL_NESTING,
} cbm_type_scope_source_t;
/* The import-map key of an open import (see open_imports). */
#define CBM_TYPE_SCOPE_OPEN_KEY "*"
/* How one language writes type names, and what its engine consumes. */
typedef struct {
CBMLanguage lang;
cbm_type_scope_source_t scope_source;
/* A name inside quotes is a type name (Python forward references); in
* every other language a quoted run is a literal and is skipped. */
bool quoted_names;
/* `::` joins the parts of a qualified name as `.` does (Rust, C++, Ruby
* `a::B`): the spelling stays as written, its QN joins with `.`. */
bool colon_paths;
/* Labels of the definitions an identity may name: the type kinds the
* engine registers and can look members up on. NULL-terminated. */
const char *const *labels;
@@ -74,7 +96,28 @@ typedef struct {
* a module (a class `Thing` in pkg/a/__init__.py beside pkg/a/Thing.py:
* their members share QNs, so no member answer is that class's). */
bool lexical_scopes;
/* Which CBMLSPDef texts hold raw type spellings for this language. */
/* Open (on-demand) imports (Java and Kotlin `.*`, C# `using N;`): each
* import-map entry under CBM_TYPE_SCOPE_OPEN_KEY names a package whose
* every type is visible. A bare name the own module and the named
* imports leave unbound is `<package>.<name>` in each open package: one
* target across all of them is its identity, two or more are none.
* Without the flag the key is an ordinary local name. */
bool open_imports;
/* The language's implicit external scope (java.lang, kotlin.*, the C#
* global namespace): true when a bare name is declared there. A name the
* own module and the named imports leave unbound and this answers true
* for has no identity (it is the external type, or ambiguous with an open
* import). NULL: no such scope. */
bool (*implicit_external)(const char *name, size_t len);
/* Member types (Java, C#, Kotlin): a bare name written in a definition
* nested in a type names that type's member type of the name first, then
* each enclosing type's, innermost first (`Inner` written inside `Outer`
* is `Outer.Inner`), before the module and the imports. A member type a
* supertype declares is not seen: past an enclosing type with a supertype
* the answer is a possible type, not a proven one. */
bool nested_types;
/* Which CBMLSPDef texts hold raw type spellings for this language. Every
* row states each one. */
bool return_types;
bool param_types;
bool field_types; /* "name:type|name:type" in field_defs */
@@ -106,6 +149,11 @@ typedef struct {
* labels), so a QN that is a module's gets no identity; NULL = not
* checked. */
const struct cbm_gbuf *graph;
/* CBM_TYPE_SCOPE_INCLUDE_CLOSURE only: the one definition named `name`
* (len bytes) the file sees through its include closure, or NULL (none,
* several, or the closure is incomplete). */
const char *(*visible)(void *ud, const char *name, size_t len);
void *visible_ud;
} cbm_type_scope_t;
/* Per-file working state: the import map by key, the file's own scopes, and
@@ -128,6 +176,16 @@ const char *cbm_type_scope_resolve(cbm_type_scope_file_t *file, const char *spel
* allocated in `arena`. */
void cbm_type_scope_attach(cbm_type_scope_file_t *file, CBMArena *arena, CBMLSPDef *def);
/* The calling file's own identity table: each local name the file's named
* imports bind, with the type identity it resolves to in the file's scope
* (one answer of a label the engine registers, as for a declared type), in
* import-map order, each local once. An engine resolving a bare name at a
* call site reads it instead of an import's unverified target. The arrays
* and strings are owned by the file state (valid until file_end). Returns
* the count (0: none). */
int cbm_type_scope_file_imports(cbm_type_scope_file_t *file, const char *const **out_locals,
const char *const **out_qns);
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
/* Work counter for the growth tests: one per byte of type text scanned, per
* hash probe and per enclosing-scope step. Test builds only. */
+11 -6
View File
@@ -428,6 +428,7 @@ static int init_schema(cbm_store_t *s) {
" defs_json TEXT NOT NULL,"
" ref_bloom BLOB,"
" config_ctx TEXT NOT NULL DEFAULT '',"
" included_files TEXT NOT NULL DEFAULT '',"
" PRIMARY KEY (project, rel_path)"
");"
/* Best-effort indexing-coverage signal (#963). One row per file the
@@ -6551,11 +6552,11 @@ int cbm_store_upsert_file_hash_batch(cbm_store_t *s, const cbm_file_hash_t *hash
static int upsert_lsp_surface_row(cbm_store_t *s, const cbm_lsp_surface_row_t *row) {
const char *sql =
"INSERT INTO lsp_surface (project, rel_path, surface_sha, defs_json, ref_bloom, config_ctx)"
" VALUES (?1, ?2, ?3, ?4, ?5, ?6)"
"INSERT INTO lsp_surface (project, rel_path, surface_sha, defs_json, ref_bloom, config_ctx,"
" included_files) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)"
" ON CONFLICT(project, rel_path) DO UPDATE SET surface_sha = excluded.surface_sha,"
" defs_json = excluded.defs_json, ref_bloom = excluded.ref_bloom,"
" config_ctx = excluded.config_ctx";
" config_ctx = excluded.config_ctx, included_files = excluded.included_files";
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
store_set_error_sqlite(s, "lsp_surface upsert prepare");
@@ -6571,6 +6572,7 @@ static int upsert_lsp_surface_row(cbm_store_t *s, const cbm_lsp_surface_row_t *r
sqlite3_bind_null(stmt, ST_COL_5);
}
bind_text(stmt, ST_COL_6, row->config_ctx ? row->config_ctx : "");
bind_text(stmt, ST_COL_7, row->included_files ? row->included_files : "");
int rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE) {
@@ -6606,8 +6608,8 @@ int cbm_store_get_lsp_surfaces(cbm_store_t *s, const char *project, cbm_lsp_surf
int *count) {
*out = NULL;
*count = 0;
const char *sql = "SELECT project, rel_path, surface_sha, defs_json, ref_bloom, config_ctx"
" FROM lsp_surface WHERE project = ?1 ORDER BY rel_path";
const char *sql = "SELECT project, rel_path, surface_sha, defs_json, ref_bloom, config_ctx,"
" included_files FROM lsp_surface WHERE project = ?1 ORDER BY rel_path";
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
store_set_error_sqlite(s, "lsp_surface get prepare");
@@ -6644,11 +6646,13 @@ int cbm_store_get_lsp_surfaces(cbm_store_t *s, const char *project, cbm_lsp_surf
const void *bloom = sqlite3_column_blob(stmt, ST_COL_4);
int bloom_len = sqlite3_column_bytes(stmt, ST_COL_4);
const char *cfg = (const char *)sqlite3_column_text(stmt, ST_COL_5);
const char *inc = (const char *)sqlite3_column_text(stmt, ST_COL_6);
r->project = strdup(proj ? proj : "");
r->rel_path = strdup(rel ? rel : "");
r->surface_sha = strdup(sha ? sha : "");
r->defs_json = strdup(defs ? defs : "[]");
r->config_ctx = strdup(cfg ? cfg : "");
r->included_files = strdup(inc ? inc : "");
if (bloom && bloom_len > 0) {
void *copy = malloc((size_t)bloom_len);
if (copy) {
@@ -6658,7 +6662,7 @@ int cbm_store_get_lsp_surfaces(cbm_store_t *s, const char *project, cbm_lsp_surf
}
}
if (!r->project || !r->rel_path || !r->surface_sha || !r->defs_json || !r->config_ctx ||
(bloom && bloom_len > 0 && !r->ref_bloom)) {
!r->included_files || (bloom && bloom_len > 0 && !r->ref_bloom)) {
n++; /* count the partial row so the free below releases it */
oom = true;
break;
@@ -6707,6 +6711,7 @@ void cbm_store_free_lsp_surfaces(cbm_lsp_surface_row_t *rows, int count) {
free((void *)rows[i].defs_json);
free((void *)rows[i].ref_bloom);
free((void *)rows[i].config_ctx);
free((void *)rows[i].included_files);
}
free(rows);
}
+10 -2
View File
@@ -39,8 +39,11 @@ typedef struct cbm_store_read_scope cbm_store_read_scope_t;
* 6 #2360: File nodes record their declared package clause (`package`
* property); an incremental run reads it back to build the namespace
* map and the Scala top-level index for the files it does not
* re-extract. */
#define CBM_INDEX_FORMAT_VERSION 6
* re-extract.
* 7 #2993: lsp_surface rows record the repository files a C-family file's
* #include lookup read (`included_files`); an incremental run
* re-extracts the file when one of them changes. */
#define CBM_INDEX_FORMAT_VERSION 7
#define CBM_STORE_CANCELLED (-3)
#define CBM_STORE_SCAN_LIMIT (-4)
#define CBM_STORE_CALLBACK_ERR (-5)
@@ -113,6 +116,11 @@ typedef struct {
const void *ref_bloom; /* referenced-identifier bloom blob (may be NULL) */
int ref_bloom_len;
const char *config_ctx; /* governing-config context hash ("" = none) */
/* The repository files the file's C-family #include lookup read
* (CBMFileResult.included_files): sorted, one per line, "" = none. Not part
* of the surface: an incremental run re-extracts the file when one of
* them changes. */
const char *included_files;
} cbm_lsp_surface_row_t;
/* The definition variants that lie in a file (graph_buffer.c "Definition
+20 -9
View File
@@ -42,8 +42,8 @@ static const sc_query_t sc_queries[] = {
"(SELECT 1 FROM main.nodes n WHERE n.id=e.target_id));"},
{"file_hashes", "SELECT rel_path,sha256,size FROM main.file_hashes WHERE project=?1 "
"ORDER BY 1,2,3;"},
{"lsp_surface", "SELECT rel_path,surface_sha,defs_json,ref_bloom,config_ctx FROM "
"main.lsp_surface WHERE project=?1 ORDER BY 1,2,3,4,5;"},
{"lsp_surface", "SELECT rel_path,surface_sha,defs_json,ref_bloom,config_ctx,included_files "
"FROM main.lsp_surface WHERE project=?1 ORDER BY 1,2,3,4,5,6;"},
{"index_coverage", "SELECT rel_path,kind,detail FROM main.index_coverage WHERE project=?1 "
"ORDER BY 1,2,3;"},
{"index_coverage_meta",
@@ -87,6 +87,22 @@ static void sc_frame(cbm_sha256_ctx *ctx, unsigned char tag, const void *bytes,
}
}
/* Read-only legacy databases retain their existing content digest: a table
* that gained a column is read with its earlier query when the column is
* absent. NULL for tables without an earlier shape. */
static const char *sc_legacy_query(const char *name) {
if (strcmp(name, "index_coverage_meta") == 0) {
return "SELECT index_mode,recording_status,ignored_files_stored,"
"ignored_files_total,coverage_version,hash_records_complete FROM "
"main.index_coverage_meta WHERE project=?1;";
}
if (strcmp(name, "lsp_surface") == 0) {
return "SELECT rel_path,surface_sha,defs_json,ref_bloom,config_ctx FROM "
"main.lsp_surface WHERE project=?1 ORDER BY 1,2,3,4,5;";
}
return NULL;
}
static bool sc_table(sqlite3 *db, const sc_query_t *q, const char *project, cbm_sha256_ctx *ctx) {
sqlite3_stmt *st = NULL;
if (sqlite3_prepare_v2(db, q->sql, -1, &st, NULL) != SQLITE_OK) {
@@ -105,13 +121,8 @@ static bool sc_table(sqlite3 *db, const sc_query_t *q, const char *project, cbm_
sqlite3_step(schema) == SQLITE_DONE;
return sqlite3_finalize(schema) == SQLITE_OK && absent;
}
/* Read-only legacy databases retain their existing content digest. */
if (strcmp(q->name, "index_coverage_meta") != 0 ||
sqlite3_prepare_v2(db,
"SELECT index_mode,recording_status,ignored_files_stored,"
"ignored_files_total,coverage_version,hash_records_complete FROM "
"main.index_coverage_meta WHERE project=?1;",
SC_SQL_TEXT_LENGTH, &st, NULL) != SQLITE_OK) {
const char *legacy = sc_legacy_query(q->name);
if (!legacy || sqlite3_prepare_v2(db, legacy, SC_SQL_TEXT_LENGTH, &st, NULL) != SQLITE_OK) {
sqlite3_finalize(st);
return false;
}
+38 -9
View File
@@ -18,6 +18,7 @@ enum {
GD_ALLOW_BLOB = 1 << GD_BLOB,
GD_TABLE_COUNT = 10,
GD_MAX_COLUMNS = 10,
GD_LSP_SURFACE = 5,
GD_COVERAGE_META = 7,
GD_STORE_META = 8,
GD_UNRESOLVED_CANDIDATES = 9,
@@ -78,8 +79,9 @@ static const gd_column_t gd_project_summaries[] = {{"project", GD_T, 0, 1},
{"created_at", GD_T, 0, 0},
{"updated_at", GD_T, 0, 0}};
static const gd_column_t gd_lsp_surface[] = {
{"project", GD_T, 0, 1}, {"rel_path", GD_T, 0, 2}, {"surface_sha", GD_T, 0, 0},
{"defs_json", GD_T, 0, 0}, {"ref_bloom", GD_B | GD_N, 0, 0}, {"config_ctx", GD_T, 0, 0}};
{"project", GD_T, 0, 1}, {"rel_path", GD_T, 0, 2}, {"surface_sha", GD_T, 0, 0},
{"defs_json", GD_T, 0, 0}, {"ref_bloom", GD_B | GD_N, 0, 0}, {"config_ctx", GD_T, 0, 0},
{"included_files", GD_T, 0, 0}};
static const gd_column_t gd_index_coverage[] = {{"project", GD_T, 0, 1},
{"rel_path", GD_T, 0, 2},
{"kind", GD_T, 0, 3},
@@ -119,7 +121,7 @@ static const gd_table_t gd_tables[GD_TABLE_COUNT] = {
"PRAGMA main.table_xinfo('project_summaries');",
"SELECT project,summary,source_hash,created_at,updated_at FROM main.project_summaries "
"ORDER BY CAST(project AS BLOB) ASC;"},
{"lsp_surface", gd_lsp_surface, GD_COLUMNS(gd_lsp_surface),
{"lsp_surface", gd_lsp_surface, GD_COLUMNS(gd_lsp_surface) - 1,
"PRAGMA main.table_xinfo('lsp_surface');",
"SELECT project,rel_path,surface_sha,defs_json,ref_bloom,config_ctx FROM main.lsp_surface "
"ORDER BY CAST(project AS BLOB) ASC,CAST(rel_path AS BLOB) ASC;"},
@@ -150,6 +152,14 @@ static const gd_table_t gd_coverage_meta_current = {
"ignored_files_total,coverage_version,hash_records_complete,unresolved_calls_complete "
"FROM main.index_coverage_meta ORDER BY CAST(project AS BLOB) ASC;"};
/* The same for lsp_surface: index format 7 records the files each C-family
* file's #include lookup read (`included_files`). */
static const gd_table_t gd_lsp_surface_current = {
"lsp_surface", gd_lsp_surface, GD_COLUMNS(gd_lsp_surface),
"PRAGMA main.table_xinfo('lsp_surface');",
"SELECT project,rel_path,surface_sha,defs_json,ref_bloom,config_ctx,included_files "
"FROM main.lsp_surface ORDER BY CAST(project AS BLOB) ASC,CAST(rel_path AS BLOB) ASC;"};
#undef GD_B
#undef GD_N
#undef GD_T
@@ -168,11 +178,28 @@ typedef struct {
bool have_uid;
bool have_counter;
bool have_unresolved_completeness;
bool have_included_files;
} gd_context_t;
static const gd_table_t *gd_table(const gd_context_t *g, int index) {
return index == GD_COVERAGE_META && g->have_unresolved_completeness ? &gd_coverage_meta_current
: &gd_tables[index];
if (index == GD_COVERAGE_META && g->have_unresolved_completeness) {
return &gd_coverage_meta_current;
}
if (index == GD_LSP_SURFACE && g->have_included_files) {
return &gd_lsp_surface_current;
}
return &gd_tables[index];
}
/* The newest schema a table may have, when it has a legacy one too. */
static const gd_table_t *gd_current_table(const gd_table_t *table) {
if (table == &gd_tables[GD_COVERAGE_META]) {
return &gd_coverage_meta_current;
}
if (table == &gd_tables[GD_LSP_SURFACE]) {
return &gd_lsp_surface_current;
}
return NULL;
}
typedef struct {
@@ -460,9 +487,8 @@ static bool gd_column_schema(gd_context_t *g, const gd_column_t *expected) {
static bool gd_columns(gd_context_t *g, const gd_table_t *table) {
bool seen[GD_MAX_COLUMNS] = {false};
bool coverage_meta = table == &gd_tables[GD_COVERAGE_META];
int allowed_columns =
coverage_meta ? gd_coverage_meta_current.column_count : table->column_count;
const gd_table_t *current = gd_current_table(table);
int allowed_columns = current ? current->column_count : table->column_count;
if (!gd_prepare(g, table->column_query)) {
return false;
}
@@ -498,9 +524,12 @@ static bool gd_columns(gd_context_t *g, const gd_table_t *table) {
return gd_fail(g, CBM_STORE_GRAPH_DIGEST_SCHEMA);
}
}
if (coverage_meta) {
if (table == &gd_tables[GD_COVERAGE_META]) {
g->have_unresolved_completeness = seen[table->column_count];
}
if (table == &gd_tables[GD_LSP_SURFACE]) {
g->have_included_files = seen[table->column_count];
}
return gd_finalize(g);
}
+56
View File
@@ -0,0 +1,56 @@
/*
* image_probe_main.c — entry point of the image probe (tests/test_image_probe.h),
* the small executable that copied-image and install tests run instead of the
* test runner. Its roles live in tests/image_probe_roles.c.
*/
#include "test_image_probe.h"
#include <stdio.h>
#include <string.h>
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
#define PROBE_MSAN_MIMALLOC_OUTPUT 1
#endif
#endif
#ifdef PROBE_MSAN_MIMALLOC_OUTPUT
#include <mimalloc.h>
#include <sanitizer/msan_interface.h>
/* MSan lane only, exactly as the runner's tf_msan_mi_output (tests/test_main.c):
* mimalloc is uninstrumented there, so text it formats on its own stack must
* be marked initialized before it reaches an MSan interceptor. */
static bool probe_msan_mi_output_live;
static void probe_msan_mi_output(const char *msg, void *arg) {
(void)arg;
if (!probe_msan_mi_output_live || !msg) {
return;
}
__msan_unpoison_string(msg);
if (!msg[0]) {
return;
}
(void)fputs(msg, stderr);
}
#endif
int main(int argc, char **argv) {
#ifdef PROBE_MSAN_MIMALLOC_OUTPUT
mi_register_output(probe_msan_mi_output, NULL);
probe_msan_mi_output_live = true;
#endif
/* An install verifies the candidate it staged by running it with
* --version (src/cli/cli.c); the probe stands in for that binary. */
if (argc == 2 && strcmp(argv[1], "--version") == 0) {
(void)puts("codebase-memory-mcp test image probe");
return 0;
}
int role_rc = tf_image_probe_dispatch(argc, argv);
if (role_rc >= 0) {
return role_rc;
}
(void)fprintf(stderr, "image probe: no role named by argv[1] (tests/test_image_probe.h)\n");
return 2;
}
+342
View File
@@ -0,0 +1,342 @@
/*
* image_probe_roles.c — the roles of the image probe (tests/test_image_probe.h).
*
* Linked into the probe (tests/image_probe_main.c) and into the test runner.
* The runner dispatches only the image holder from here (daemon_runtime's
* own-image cache test spawns the runner itself); its role table in
* tests/test_main.c names every other literal too, so that the test-impact
* engine still maps a change these roles reach to the tests that start them.
* Every role is called directly from tf_image_probe_dispatch for the same
* reason: a call edge, not a function-pointer table, links them.
*
* Exit codes: 0 success; 24/25 holder failures; 26 the daemon ANSWERED a HELLO
* and refused it; 27 the HELLO session did not close; 28 the macOS mapping
* failed; 29 the daemon answered an activation request and refused it; 30/31 a
* HELLO/activation that got no answer inside the exchange budget (a slow peer
* hash fails as itself, never as a refusal); 32 bad serve arguments; 33 the
* served daemon did not start; 34 its teardown failed.
*/
#include "test_image_probe.h"
#include "test_daemon_runtime_contract.h"
#include "daemon/daemon.h"
#include "daemon/ipc.h"
#include "daemon/runtime.h"
#include "foundation/compat.h"
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#include <windows.h>
#else
#include <unistd.h>
#ifdef __APPLE__
#include <fcntl.h>
#include <sys/mman.h>
#endif
#endif
enum {
/* The in-process daemon_runtime fixture's bounds (RUNTIME_TEST_TIMEOUT_MS,
* RUNTIME_TEST_CLEANUP_TIMEOUT_MS, RUNTIME_TEST_CLEANUP_FREE_ATTEMPTS):
* the served daemon behaves exactly like that fixture's. */
PROBE_TIMEOUT_MS = 2000,
PROBE_CLEANUP_TIMEOUT_MS = 60000,
PROBE_CLEANUP_FREE_ATTEMPTS = 3,
PROBE_PATH_CAP = 1024,
};
static uint64_t probe_process_id(void) {
#ifdef _WIN32
return (uint64_t)GetCurrentProcessId();
#else
return (uint64_t)getpid();
#endif
}
static int probe_role_holder(int argc, char **argv) {
#ifdef _WIN32
if (argc != 3) {
return -1;
}
HANDLE ready = OpenEventA(EVENT_MODIFY_STATE, FALSE, argv[2]);
bool announced = ready && SetEvent(ready) != 0;
if (ready) {
(void)CloseHandle(ready);
}
if (!announced) {
return 24;
}
Sleep(INFINITE);
return 25;
#else
/* POSIX copied-image holder: block reading stdin until the parent closes
* the release pipe, exactly like the cat(1) donor this replaced. A system
* utility cannot serve as the copied image — a multi-call coreutils
* binary (uutils cat) refuses to execute under the copied name. */
(void)argv;
if (argc != 2) {
return -1;
}
char release[16];
ssize_t count;
do {
count = read(STDIN_FILENO, release, sizeof(release));
} while (count > 0 || (count < 0 && errno == EINTR));
return count == 0 ? 0 : 25;
#endif
}
/* Real copied-image HELLO probe: the daemon authenticates and fingerprints an
* ordinary, separately executed process image; no production-only hook. */
static int probe_role_hello(int argc, char **argv) {
if (argc != 6) {
return -1;
}
cbm_daemon_ipc_endpoint_t *endpoint = cbm_daemon_ipc_endpoint_new(argv[3], argv[2]);
cbm_daemon_build_identity_t identity = {
.semantic_version = argv[4],
.build_fingerprint = argv[5],
};
cbm_daemon_runtime_connect_result_t result;
memset(&result, 0, sizeof(result));
cbm_daemon_runtime_client_t *client =
endpoint ? cbm_daemon_runtime_client_connect(endpoint, &identity,
TF_RUNTIME_IMAGE_EXCHANGE_TIMEOUT_MS, &result)
: NULL;
bool accepted = client && result.status == CBM_DAEMON_RUNTIME_CONNECT_ACCEPTED &&
result.hello_status == CBM_DAEMON_HELLO_COMPATIBLE;
bool closed =
!client || cbm_daemon_runtime_client_close(client, TF_RUNTIME_IMAGE_EXCHANGE_TIMEOUT_MS);
cbm_daemon_ipc_endpoint_free(endpoint);
if (!accepted) {
/* 26 only when the daemon ANSWERED and refused. No answer inside the
* exchange budget (the daemon still hashing this copy, or I/O) is 30:
* a slow peer-image hash must fail loudly as itself, never satisfy a
* rejection assertion by timing out. */
return result.status == CBM_DAEMON_RUNTIME_CONNECT_ERROR ? 30 : 26;
}
return closed ? 0 : 27;
}
/* Real copied-image activation probe. The request identity is supplied by the
* executing copy so the daemon must authenticate that peer image rather than
* require the active generation's build fingerprint. */
static int probe_role_activation(int argc, char **argv) {
if (argc != 7) {
return -1;
}
char *action_end = NULL;
unsigned long action_value = strtoul(argv[6], &action_end, 10);
bool action_valid = action_end != argv[6] && *action_end == '\0' &&
action_value >= (unsigned long)CBM_DAEMON_RUNTIME_ACTIVATION_INSTALL &&
action_value <= (unsigned long)CBM_DAEMON_RUNTIME_ACTIVATION_UNINSTALL;
cbm_daemon_ipc_endpoint_t *endpoint =
action_valid ? cbm_daemon_ipc_endpoint_new(argv[3], argv[2]) : NULL;
cbm_daemon_build_identity_t identity = {
.semantic_version = argv[4],
.build_fingerprint = argv[5],
};
cbm_daemon_runtime_activation_result_t result;
memset(&result, 0, sizeof(result));
bool exchanged =
endpoint && cbm_daemon_runtime_request_activation_shutdown(
endpoint, &identity, (cbm_daemon_runtime_activation_action_t)action_value,
TF_RUNTIME_IMAGE_EXCHANGE_TIMEOUT_MS, &result);
cbm_daemon_ipc_endpoint_free(endpoint);
if (!exchanged) {
return 31; /* no answer inside the exchange budget; see the HELLO probe */
}
return result.accepted ? 0 : 29;
}
/* macOS adversarial HELLO probe: execute this mode from a foreign process
* image while mapping the genuine daemon image RX. The daemon must
* authenticate the main image, not merely find its own vnode in an arbitrary
* executable map. */
static int probe_role_mapped_hello(int argc, char **argv) {
#ifdef __APPLE__
if (argc != 7) {
return -1;
}
int image_fd = open(argv[2], O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
void *mapping =
image_fd >= 0 ? mmap(NULL, 1, PROT_READ | PROT_EXEC, MAP_PRIVATE, image_fd, 0) : MAP_FAILED;
bool mapped = mapping != MAP_FAILED;
if (image_fd >= 0 && close(image_fd) != 0) {
mapped = false;
}
if (!mapped) {
if (mapping != MAP_FAILED) {
(void)munmap(mapping, 1);
}
return 28;
}
char *hello_argv[] = {argv[0], "__cbm_runtime_hello_client", argv[3], argv[4], argv[5],
argv[6]};
int result = probe_role_hello(6, hello_argv);
if (munmap(mapping, 1) != 0) {
return 28;
}
return result >= 0 ? result : 28;
#else
(void)argc;
(void)argv;
return -1;
#endif
}
static void probe_reply(const char *line) {
(void)fputs(line, stdout);
(void)fputc('\n', stdout);
(void)fflush(stdout);
}
/* The daemon of the copied-image tests, running from THIS image:
* __cbm_runtime_serve <parent> <key> <version> <owner:0|1>
* A runtime service starts only with its own image's fingerprint as identity,
* so a daemon whose peers are copies of this probe must run here, not in the
* runner. The configuration is the in-process daemon_runtime fixture's
* (runtime_test_fixture_start, ABIs of runtime_test_identity), and with owner 1
* a committed session of the active image is held in this process, like that
* fixture's in-process owner client.
*
* One line answers each step on stdout; commands arrive on stdin, one byte:
* start "ready <own fingerprint>" or "failed <stage>"
* 'i' "1" when the held owner session was interrupted (its heartbeat
* fails), else "0"
* 'x' closes the owner session, then "1" when the service has exited by
* itself within the fixture's bound, else "0"
* EOF the runner's release: stop what still runs, free it, exit.
* No step waits on a clock of its own: every bound is the runtime API's, and
* the runner closing stdin ends the role. */
static int probe_role_serve(int argc, char **argv) {
if (argc != 6 || (strcmp(argv[5], "0") != 0 && strcmp(argv[5], "1") != 0)) {
return 32;
}
#ifdef _WIN32
/* Exact bytes both ways: no CRLF translation on the protocol pipes. */
(void)_setmode(_fileno(stdin), _O_BINARY);
(void)_setmode(_fileno(stdout), _O_BINARY);
#endif
bool with_owner = argv[5][0] == '1';
char build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE] = {0};
char log_path[PROBE_PATH_CAP];
int log_written = snprintf(log_path, sizeof(log_path), "%s/conflicts.ndjson", argv[2]);
cbm_daemon_ipc_endpoint_t *endpoint = cbm_daemon_ipc_endpoint_new(argv[3], argv[2]);
const char *failed = NULL;
if (log_written <= 0 || log_written >= (int)sizeof(log_path)) {
failed = "conflict-log-path";
} else if (!endpoint) {
failed = "endpoint";
} else if (!cbm_daemon_runtime_process_build_fingerprint(probe_process_id(), build)) {
failed = "own-image-fingerprint";
}
cbm_daemon_build_identity_t identity = {
.semantic_version = argv[4],
.build_fingerprint = build,
.protocol_abi = 3,
.store_abi = 11,
.feature_abi = 7,
};
cbm_daemon_runtime_service_t *service = NULL;
if (!failed) {
cbm_daemon_runtime_service_config_t config = {
.endpoint = endpoint,
.identity = identity,
.conflict_log_path = log_path,
.conflict_log_cap_bytes = 64U * 1024U,
.max_clients = 8,
.lease_timeout_ms = 5000,
.request_timeout_ms = PROBE_TIMEOUT_MS,
.shutdown_timeout_ms = PROBE_TIMEOUT_MS,
};
service = cbm_daemon_runtime_service_start(&config);
if (!service ||
cbm_daemon_runtime_service_state(service) != CBM_DAEMON_RUNTIME_SERVICE_RUNNING) {
failed = "service-start";
}
}
cbm_daemon_runtime_client_t *owner = NULL;
if (!failed && with_owner) {
cbm_daemon_runtime_connect_result_t result;
memset(&result, 0, sizeof(result));
owner = cbm_daemon_runtime_client_connect(endpoint, &identity, PROBE_TIMEOUT_MS, &result);
if (!owner || result.status != CBM_DAEMON_RUNTIME_CONNECT_ACCEPTED) {
failed = "owner-connect";
}
}
char line[CBM_DAEMON_BUILD_FINGERPRINT_SIZE + 16];
(void)snprintf(line, sizeof(line), "%s %s", failed ? "failed" : "ready",
failed ? failed : build);
probe_reply(line);
for (int command = failed ? EOF : getchar(); command != EOF; command = getchar()) {
if (command == 'i') {
bool interrupted =
owner && !cbm_daemon_runtime_client_heartbeat(owner, PROBE_TIMEOUT_MS);
probe_reply(interrupted ? "1" : "0");
} else if (command == 'x') {
if (owner) {
(void)cbm_daemon_runtime_client_close(owner, PROBE_TIMEOUT_MS);
owner = NULL;
}
probe_reply(cbm_daemon_runtime_service_wait_exited(service, PROBE_TIMEOUT_MS) ? "1"
: "0");
}
}
if (owner) {
(void)cbm_daemon_runtime_client_close(owner, PROBE_TIMEOUT_MS);
}
bool freed = service == NULL;
if (service) {
/* runtime_test_fixture_finish's teardown, unchanged. */
bool stopped =
cbm_daemon_runtime_service_state(service) == CBM_DAEMON_RUNTIME_SERVICE_EXITED ||
cbm_daemon_runtime_service_stop(service, PROBE_CLEANUP_TIMEOUT_MS);
for (int attempt = 0; stopped && !freed && attempt < PROBE_CLEANUP_FREE_ATTEMPTS;
attempt++) {
freed = cbm_daemon_runtime_service_free(service);
if (!freed) {
cbm_usleep(1000);
}
}
}
cbm_daemon_ipc_endpoint_free(endpoint);
if (failed) {
return 33;
}
return freed ? 0 : 34;
}
int tf_image_probe_dispatch(int argc, char **argv) {
if (argc < 2 || !argv || !argv[1]) {
return -1;
}
const char *role = argv[1];
if (strcmp(role, "__cbm_runtime_image_holder") == 0) {
return probe_role_holder(argc, argv);
}
if (strcmp(role, "__cbm_runtime_serve") == 0) {
return probe_role_serve(argc, argv);
}
if (strcmp(role, "__cbm_runtime_hello_client") == 0) {
return probe_role_hello(argc, argv);
}
if (strcmp(role, "__cbm_runtime_activation_client") == 0) {
return probe_role_activation(argc, argv);
}
if (strcmp(role, "__cbm_runtime_mapped_hello_client") == 0) {
return probe_role_mapped_hello(argc, argv);
}
return -1;
}
+6 -1
View File
@@ -140,7 +140,9 @@ TEST(invariant_discovery_always_skip_dirs) {
{ ".ruff_cache", 1 }, /* GREEN */
{ ".eggs", 1 }, /* GREEN */
{ ".env", 1 }, /* GREEN */
{ "env", 1 }, /* GREEN */
/* "env" left this table on 2026-11-17 (maintainer decision): it is
* skipped only as a Python virtual environment, pinned by
* discover_env_skipped_only_as_virtualenv in tests/test_discover.c. */
{ "htmlcov", 1 }, /* GREEN */
{ "site-packages", 1 }, /* GREEN */
@@ -193,6 +195,9 @@ TEST(invariant_discovery_always_skip_dirs) {
/* Deploy */
{ ".vercel", 1 }, /* GREEN */
{ ".netlify", 1 }, /* GREEN */
/* The canary is program source (stub.py), which deploy/ and deployed/
* keep skipping; their configuration files are indexed
* (discover_deploy_indexes_config_files_only). */
{ "deploy", 1 }, /* GREEN */
{ "deployed", 1 }, /* GREEN */
+40 -20
View File
@@ -13,6 +13,7 @@
#include "../src/foundation/compat_thread.h"
#include "test_framework.h"
#include "test_helpers.h"
#include "test_image_probe.h"
#include <cli/agent_profiles.h>
#include <cli/activation_transaction.h>
#include <cli/cli.h>
@@ -917,6 +918,25 @@ static int cli_test_cmd_install(int argc, char **argv) {
return cli_test_cmd_dispatch(cbm_cmd_install, argc, argv);
}
/* An install whose running binary is the image probe (tests/test_image_probe.h),
* not this runner. These tests need a real executable to stage, verify (it is
* run with --version) and publish, never this one: each install of the ~800 MB
* sanitized runner made up to five full SHA-256 passes and two fsync'd staged
* copies of it, and one such hash costs 70-95 s on some hosted x86 CPUs. -1,
* said on stderr, when the probe was not built. */
static int cli_test_cmd_install_probe(int argc, char **argv) {
static char probe[1024];
if (!probe[0] && !th_image_probe_path(probe, sizeof(probe))) {
(void)fprintf(stderr, "cli: no image probe beside this runner; it is built with the "
"runner (Makefile.cbm, <runner>-image-probe)\n");
return -1;
}
cbm_cli_set_self_path_override_for_test(probe);
int rc = cli_test_cmd_install(argc, argv);
cbm_cli_set_self_path_override_for_test(NULL);
return rc;
}
static int cli_test_cmd_uninstall(int argc, char **argv) {
return cli_test_cmd_dispatch(cbm_cmd_uninstall, argc, argv);
}
@@ -1448,7 +1468,7 @@ TEST(cli_activation_quiesce_does_not_wait_on_bootstrap_startup) {
char dir_arg[640];
snprintf(dir_arg, sizeof(dir_arg), "--dir=%s", install_dir);
char *install_argv[] = {"--force", "--skip-config", "--yes", dir_arg};
int install_rc = child_ready ? cli_test_cmd_install(4, install_argv) : -1;
int install_rc = child_ready ? cli_test_cmd_install_probe(4, install_argv) : -1;
close(install_done_pipe[1]); /* the child's watch ends here at the latest */
cbm_cli_set_activation_runtime_parent_for_test(g_cli_suite_runtime_parent);
cbm_set_auto_answer_for_test(0);
@@ -1559,7 +1579,7 @@ TEST(cli_install_recovers_markerless_stale_rendezvous) {
char dir_arg[640];
snprintf(dir_arg, sizeof(dir_arg), "--dir=%s", install_dir);
char *install_argv[] = {"--force", "--skip-config", "--yes", dir_arg};
int install_rc = orphan_created ? cli_test_cmd_install(4, install_argv) : -1;
int install_rc = orphan_created ? cli_test_cmd_install_probe(4, install_argv) : -1;
cbm_cli_set_activation_runtime_parent_for_test(g_cli_suite_runtime_parent);
cbm_set_auto_answer_for_test(0);
@@ -1969,7 +1989,7 @@ static int cli_scope_install(cli_scope_fixture_t *fixture, const char *home, con
snprintf(dir_arg, sizeof(dir_arg), "--dir=%s", bin_dir);
char *install_argv[] = {skip_binary ? "--skip-binary" : "--force", "--skip-config", "--yes",
dir_arg};
int rc = cli_test_cmd_install(4, install_argv);
int rc = cli_test_cmd_install_probe(4, install_argv);
cbm_cli_set_activation_runtime_parent_for_test(g_cli_suite_runtime_parent);
cbm_set_auto_answer_for_test(0);
return rc;
@@ -2393,7 +2413,7 @@ TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--force"};
int rc = cli_test_cmd_install(1, argv);
int rc = cli_test_cmd_install_probe(1, argv);
cbm_cli_set_activation_ops_for_test(NULL);
const char *installed = read_test_file(bin_target);
@@ -2445,11 +2465,11 @@ TEST(cli_install_dir_and_skip_config_stage_first_install_safely) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--force", "--skip-config", "--yes", "--dir", install_dir};
int rc = cli_test_cmd_install(5, argv);
int rc = cli_test_cmd_install_probe(5, argv);
char equals_arg[640];
snprintf(equals_arg, sizeof(equals_arg), "--dir=%s", install_dir);
char *dry_argv[] = {"--force", "--skip-config", "--dry-run", equals_arg};
int dry_rc = cli_test_cmd_install(4, dry_argv);
int dry_rc = cli_test_cmd_install_probe(4, dry_argv);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -2853,12 +2873,12 @@ TEST(cli_no_hooks_removes_codex_pretool_hook) {
size_t owned = 0U;
size_t foreign = 0U;
char *default_args[] = {"--skip-binary", "--clients=codex", "--yes"};
int default_rc = cli_test_cmd_install(3, default_args);
int default_rc = cli_test_cmd_install_probe(3, default_args);
test_codex_pretool_hook_counts(hooks_path, foreign_command, &owned, &foreign);
bool default_json = default_rc == 0 && owned == 1U && foreign == 1U;
char *no_hooks_args[] = {"--no-hooks", "--skip-binary", "--clients=codex", "--yes"};
int no_hooks_rc = cli_test_cmd_install(4, no_hooks_args);
int no_hooks_rc = cli_test_cmd_install_probe(4, no_hooks_args);
test_codex_pretool_hook_counts(hooks_path, foreign_command, &owned, &foreign);
char *json_state = read_test_file_alloc(hooks_path);
char *toml_state = read_test_file_alloc(config_path);
@@ -2869,26 +2889,26 @@ TEST(cli_no_hooks_removes_codex_pretool_hook) {
free(json_state);
free(toml_state);
int bare_rc = cli_test_cmd_install(3, default_args);
int bare_rc = cli_test_cmd_install_probe(3, default_args);
test_codex_pretool_hook_counts(hooks_path, foreign_command, &owned, &foreign);
bool bare_disabled = bare_rc == 0 && owned == 0U && foreign == 1U;
/* Without hooks.json the config.toml managed block carries the hooks. */
(void)remove(hooks_path);
char *hooks_args[] = {"--hooks", "--skip-binary", "--clients=codex", "--yes"};
int toml_enable_rc = cli_test_cmd_install(4, hooks_args);
int toml_enable_rc = cli_test_cmd_install_probe(4, hooks_args);
toml_state = read_test_file_alloc(config_path);
bool toml_enabled = toml_enable_rc == 0 && toml_state &&
test_count_substring(toml_state, "[[hooks.PreToolUse]]") == 1U;
free(toml_state);
int toml_disable_rc = cli_test_cmd_install(4, no_hooks_args);
int toml_disable_rc = cli_test_cmd_install_probe(4, no_hooks_args);
toml_state = read_test_file_alloc(config_path);
bool toml_disabled = toml_disable_rc == 0 && toml_state &&
!strstr(toml_state, "hooks.PreToolUse") &&
!strstr(toml_state, "hook-augment") &&
strstr(toml_state, "[mcp_servers.codebase-memory-mcp]");
free(toml_state);
int restore_rc = cli_test_cmd_install(4, hooks_args);
int restore_rc = cli_test_cmd_install_probe(4, hooks_args);
toml_state = read_test_file_alloc(config_path);
bool restored = restore_rc == 0 && toml_state &&
test_count_substring(toml_state, "[[hooks.PreToolUse]]") == 1U;
@@ -2969,12 +2989,12 @@ TEST(cli_no_hooks_removes_augment_owned_scripts) {
cbm_cli_set_activation_ops_for_test(&ops);
char *default_args[] = {"--skip-binary", "--clients=augment", "--yes"};
int default_rc = cli_test_cmd_install(3, default_args);
int default_rc = cli_test_cmd_install_probe(3, default_args);
bool default_scripts =
default_rc == 0 && cbm_file_exists(session_path) && cbm_file_exists(coverage_path);
char *no_hooks_args[] = {"--no-hooks", "--skip-binary", "--clients=augment", "--yes"};
int no_hooks_rc = cli_test_cmd_install(4, no_hooks_args);
int no_hooks_rc = cli_test_cmd_install_probe(4, no_hooks_args);
bool scripts_removed =
no_hooks_rc == 0 && !cbm_file_exists(session_path) && !cbm_file_exists(coverage_path);
char *settings = read_test_file_alloc(settings_path);
@@ -2984,12 +3004,12 @@ TEST(cli_no_hooks_removes_augment_owned_scripts) {
!strstr(settings, "codebase-memory-coverage");
free(settings);
int bare_rc = cli_test_cmd_install(3, default_args);
int bare_rc = cli_test_cmd_install_probe(3, default_args);
bool bare_disabled =
bare_rc == 0 && !cbm_file_exists(session_path) && !cbm_file_exists(coverage_path);
char *hooks_args[] = {"--hooks", "--skip-binary", "--clients=augment", "--yes"};
int hooks_rc = cli_test_cmd_install(4, hooks_args);
int hooks_rc = cli_test_cmd_install_probe(4, hooks_args);
settings = read_test_file_alloc(settings_path);
bool restored = hooks_rc == 0 && cbm_file_exists(session_path) &&
cbm_file_exists(coverage_path) && settings &&
@@ -3005,7 +3025,7 @@ TEST(cli_no_hooks_removes_augment_owned_scripts) {
owned_session ? owned_session : "");
free(owned_session);
write_test_file(session_path, user_session);
int user_rc = cli_test_cmd_install(4, no_hooks_args);
int user_rc = cli_test_cmd_install_probe(4, no_hooks_args);
char *kept_session = read_test_file_alloc(session_path);
bool user_script_kept = user_rc == 0 && kept_session &&
strcmp(kept_session, user_session) == 0 &&
@@ -3015,7 +3035,7 @@ TEST(cli_no_hooks_removes_augment_owned_scripts) {
/* End on --hooks, as the Codex case does: the opt-out is process state,
* and later tests install without the command line. */
(void)remove(session_path);
int final_rc = cli_test_cmd_install(4, hooks_args);
int final_rc = cli_test_cmd_install_probe(4, hooks_args);
bool final_restored =
final_rc == 0 && cbm_file_exists(session_path) && cbm_file_exists(coverage_path);
@@ -3075,7 +3095,7 @@ TEST(cli_install_reset_deletion_waits_for_final_activation_guard) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--force", "--reset-indexes", "--yes"};
int rc = cli_test_cmd_install(3, argv);
int rc = cli_test_cmd_install_probe(3, argv);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
@@ -3393,7 +3413,7 @@ TEST(cli_install_config_failure_keeps_published_binary) {
cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
cbm_cli_set_activation_ops_for_test(&ops);
char *argv[] = {"--force", "--yes"};
int rc = cli_test_cmd_install(2, argv);
int rc = cli_test_cmd_install_probe(2, argv);
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);
+442 -84
View File
@@ -8,6 +8,7 @@
#include "test_framework.h"
#include "test_helpers.h"
#include "test_daemon_runtime_contract.h"
#include "test_image_probe.h"
#include "daemon/application.h"
#include "daemon/host.h"
@@ -37,6 +38,7 @@
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include "foundation/subprocess.h" /* cbm_build_win_cmdline */
#include "foundation/win_utf8.h"
#include <stddef.h>
#include <windows.h>
@@ -505,6 +507,37 @@ static bool runtime_test_copy_self_image(const char *destination) {
#endif
}
/* The image probe beside this runner (tests/test_image_probe.h): the small
* executable the copied-image tests copy, hash and run instead of this runner.
* NULL, said once on stderr, when it was not built. */
static const char *runtime_test_probe_image(void) {
static char path[RUNTIME_TEST_PATH_CAP];
if (!path[0] && !th_image_probe_path(path, sizeof(path))) {
fprintf(stderr, "daemon_runtime: no image probe beside this runner; it is built "
"with the runner (Makefile.cbm, <runner>-image-probe)\n");
return NULL;
}
return path;
}
static bool runtime_test_copy_probe_image(const char *destination) {
const char *probe = runtime_test_probe_image();
#ifdef _WIN32
wchar_t *source_wide = probe && destination ? cbm_path_to_wide(probe) : NULL;
wchar_t *destination_wide = source_wide ? cbm_path_to_wide(destination) : NULL;
bool copied = destination_wide && CopyFileW(source_wide, destination_wide, TRUE) != 0;
free(destination_wide);
free(source_wide);
return copied;
#elif defined(__APPLE__) || defined(__linux__)
return probe && destination && runtime_test_copy_executable(probe, destination);
#else
(void)probe;
(void)destination;
return false;
#endif
}
static bool runtime_test_paths_refer_to_same_file(const char *left, const char *right) {
enum { CANONICAL_CAP = 4096 };
char left_canonical[CANONICAL_CAP];
@@ -1287,10 +1320,10 @@ typedef struct {
* fixture's convenience start also joins a service-owned participant guard. */
static runtime_test_coordination_t *runtime_test_fixture_coordination = NULL;
static bool runtime_test_fixture_start_configured(
runtime_test_fixture_t *fixture, const char *tag, const cbm_daemon_build_identity_t *identity,
uint32_t max_clients, uint64_t lease_timeout_ms,
const cbm_daemon_runtime_application_callbacks_t *application) {
/* The private parent, rendezvous key, endpoint and log paths of a fixture,
* before any daemon serves it: shared by the in-process service below and the
* image probe's daemon (runtime_test_probe_daemon_start). */
static bool runtime_test_fixture_prepare(runtime_test_fixture_t *fixture, const char *tag) {
memset(fixture, 0, sizeof(*fixture));
if (!th_secure_runtime_parent_new(fixture->parent, sizeof(fixture->parent), tag)) {
return runtime_test_fixture_start_failed(tag, "temporary-directory",
@@ -1325,7 +1358,16 @@ static bool runtime_test_fixture_start_configured(
if (lock_written <= 0 || lock_written >= (int)sizeof(fixture->lock_log_path)) {
return runtime_test_fixture_start_failed(tag, "lock-log-path", lock_written);
}
return true;
}
static bool runtime_test_fixture_start_configured(
runtime_test_fixture_t *fixture, const char *tag, const cbm_daemon_build_identity_t *identity,
uint32_t max_clients, uint64_t lease_timeout_ms,
const cbm_daemon_runtime_application_callbacks_t *application) {
if (!runtime_test_fixture_prepare(fixture, tag)) {
return false;
}
cbm_daemon_runtime_service_config_t config = {
.endpoint = fixture->endpoint,
.identity = *identity,
@@ -1549,6 +1591,306 @@ static void runtime_test_fixture_finish(runtime_test_fixture_t *fixture) {
memset(fixture, 0, sizeof(*fixture));
}
/* ── The image probe's daemon ────────────────────────────────────────────
* The copied-image tests need a daemon whose peers are separately executed
* copies of the daemon's own image. A runtime service starts only with its own
* image's fingerprint as identity, so that daemon runs in the small image
* probe (__cbm_runtime_serve, tests/image_probe_roles.c), not in this runner:
* it fingerprints every peer copy before answering, and a copy of this
* ~800 MB sanitized runner took 70-95 s per hash on some hosted x86 CPUs, past
* the clients' exchange budget. Paths, key, endpoint and cleanup are the
* in-process fixture's (its .service stays NULL); the daemon is driven over
* its stdin and stdout, and nothing here waits on a clock: every read ends on
* a reply or on EOF when the probe is gone. */
typedef struct {
runtime_test_fixture_t fixture;
char build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; /* its identity: the probe's fingerprint */
bool spawned;
#ifdef _WIN32
HANDLE process;
HANDLE commands; /* the probe's stdin */
HANDLE replies; /* the probe's stdout */
#else
pid_t process;
int commands;
int replies;
#endif
} runtime_test_probe_daemon_t;
#ifdef _WIN32
/* Start argv with exactly the two pipe ends (and a duplicate of this runner's
* stderr, when there is one) inherited, as tf_rss_tree_start_descendant does. */
static bool runtime_test_probe_daemon_spawn(runtime_test_probe_daemon_t *daemon,
const char *const *argv) {
SECURITY_ATTRIBUTES inheritable;
memset(&inheritable, 0, sizeof(inheritable));
inheritable.nLength = sizeof(inheritable);
inheritable.bInheritHandle = TRUE;
HANDLE child_input = NULL;
HANDLE commands = NULL;
HANDLE replies = NULL;
HANDLE child_output = NULL;
HANDLE child_error = NULL;
bool piped = CreatePipe(&child_input, &commands, &inheritable, 0) &&
CreatePipe(&replies, &child_output, &inheritable, 0) &&
SetHandleInformation(commands, HANDLE_FLAG_INHERIT, 0) &&
SetHandleInformation(replies, HANDLE_FLAG_INHERIT, 0);
HANDLE error = GetStdHandle(STD_ERROR_HANDLE);
if (piped && error && error != INVALID_HANDLE_VALUE &&
!DuplicateHandle(GetCurrentProcess(), error, GetCurrentProcess(), &child_error, 0, TRUE,
DUPLICATE_SAME_ACCESS)) {
child_error = NULL; /* the probe then runs without a stderr */
}
char command_line[RUNTIME_TEST_PATH_CAP * 4];
bool built = piped && cbm_build_win_cmdline(command_line, sizeof(command_line), argv);
wchar_t *application = built ? cbm_path_to_wide(argv[0]) : NULL;
wchar_t *command = application ? cbm_utf8_to_wide(command_line) : NULL;
HANDLE inherit[3] = {child_input, child_output, child_error};
DWORD inherit_count = child_error ? 3U : 2U;
SIZE_T attrs_size = 0;
(void)InitializeProcThreadAttributeList(NULL, 1, 0, &attrs_size);
LPPROC_THREAD_ATTRIBUTE_LIST attrs =
command ? (LPPROC_THREAD_ATTRIBUTE_LIST)malloc(attrs_size) : NULL;
bool attrs_init = attrs && InitializeProcThreadAttributeList(attrs, 1, 0, &attrs_size);
bool attrs_ready =
attrs_init && UpdateProcThreadAttribute(attrs, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
inherit, inherit_count * sizeof(HANDLE), NULL,
NULL);
STARTUPINFOEXW startup;
memset(&startup, 0, sizeof(startup));
startup.StartupInfo.cb = sizeof(startup);
startup.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
startup.StartupInfo.hStdInput = child_input;
startup.StartupInfo.hStdOutput = child_output;
startup.StartupInfo.hStdError = child_error;
startup.lpAttributeList = attrs;
PROCESS_INFORMATION process;
memset(&process, 0, sizeof(process));
bool started = attrs_ready &&
CreateProcessW(application, command, NULL, NULL, TRUE,
EXTENDED_STARTUPINFO_PRESENT | CREATE_NO_WINDOW, NULL, NULL,
&startup.StartupInfo, &process) != 0;
if (attrs_init) {
DeleteProcThreadAttributeList(attrs);
}
free(attrs);
free(command);
free(application);
HANDLE child_ends[3] = {child_input, child_output, child_error};
for (size_t i = 0; i < 3U; i++) {
if (child_ends[i]) {
(void)CloseHandle(child_ends[i]);
}
}
if (!started) {
if (commands) {
(void)CloseHandle(commands);
}
if (replies) {
(void)CloseHandle(replies);
}
return false;
}
(void)CloseHandle(process.hThread);
daemon->process = process.hProcess;
daemon->commands = commands;
daemon->replies = replies;
return true;
}
#elif defined(__APPLE__) || defined(__linux__)
/* posix_spawn, not fork, for the reason runtime_test_spawn_blocked_executable
* gives. This runner's pipe ends are close-on-exec, so no later child holds
* the daemon's stdin open past its release. */
static bool runtime_test_probe_daemon_spawn(runtime_test_probe_daemon_t *daemon,
const char *const *argv) {
int commands[2] = {-1, -1};
int replies[2] = {-1, -1};
if (pipe(commands) != 0) {
return false;
}
if (pipe(replies) != 0) {
(void)close(commands[0]);
(void)close(commands[1]);
return false;
}
bool private_ends = fcntl(commands[1], F_SETFD, FD_CLOEXEC) == 0 &&
fcntl(replies[0], F_SETFD, FD_CLOEXEC) == 0;
posix_spawn_file_actions_t actions;
bool actions_ready = private_ends && posix_spawn_file_actions_init(&actions) == 0;
bool actions_set = actions_ready &&
posix_spawn_file_actions_adddup2(&actions, commands[0], STDIN_FILENO) == 0 &&
posix_spawn_file_actions_adddup2(&actions, replies[1], STDOUT_FILENO) == 0 &&
posix_spawn_file_actions_addclose(&actions, commands[0]) == 0 &&
posix_spawn_file_actions_addclose(&actions, replies[1]) == 0;
pid_t child = -1;
bool started = actions_set && posix_spawn(&child, argv[0], &actions, NULL,
(char *const *)argv, environ) == 0;
if (actions_ready) {
(void)posix_spawn_file_actions_destroy(&actions);
}
(void)close(commands[0]);
(void)close(replies[1]);
if (!started) {
(void)close(commands[1]);
(void)close(replies[0]);
return false;
}
daemon->process = child;
daemon->commands = commands[1];
daemon->replies = replies[0];
return true;
}
#else
static bool runtime_test_probe_daemon_spawn(runtime_test_probe_daemon_t *daemon,
const char *const *argv) {
(void)daemon;
(void)argv;
return false;
}
#endif
/* One reply line, without its line end. false on EOF (the probe is gone), a
* read error, or a line longer than cap. */
static bool runtime_test_probe_daemon_read_line(runtime_test_probe_daemon_t *daemon, char *line,
size_t cap) {
size_t used = 0;
bool fits = cap > 0;
for (;;) {
char ch = 0;
#ifdef _WIN32
DWORD got = 0;
if (!ReadFile(daemon->replies, &ch, 1, &got, NULL) || got != 1) {
return false;
}
#else
ssize_t got = read(daemon->replies, &ch, 1);
if (got < 0 && errno == EINTR) {
continue;
}
if (got != 1) {
return false;
}
#endif
if (ch == '\n') {
if (fits) {
line[used] = '\0';
}
return fits;
}
if (ch != '\r' && fits && used + 1 < cap) {
line[used++] = ch;
} else if (ch != '\r') {
fits = false;
}
}
}
static bool runtime_test_probe_daemon_send(runtime_test_probe_daemon_t *daemon, char command) {
#ifdef _WIN32
DWORD written = 0;
return WriteFile(daemon->commands, &command, 1, &written, NULL) != 0 && written == 1;
#else
/* A probe that is already gone must fail the test, not kill the runner
* with SIGPIPE: ignore it for this one write, as test_subprocess does. */
struct sigaction ignore;
struct sigaction previous;
memset(&ignore, 0, sizeof(ignore));
ignore.sa_handler = SIG_IGN;
sigemptyset(&ignore.sa_mask);
if (sigaction(SIGPIPE, &ignore, &previous) != 0) {
return false;
}
ssize_t written;
do {
written = write(daemon->commands, &command, 1);
} while (written < 0 && errno == EINTR);
bool restored = sigaction(SIGPIPE, &previous, NULL) == 0;
return written == 1 && restored;
#endif
}
/* Start the probe's daemon in a fresh fixture: true once it reported "ready"
* with its fingerprint, which then is daemon->build. with_owner also holds a
* committed session of the active image inside the probe. */
static bool runtime_test_probe_daemon_start(runtime_test_probe_daemon_t *daemon, const char *tag,
const char *version, bool with_owner) {
memset(daemon, 0, sizeof(*daemon));
const char *probe = runtime_test_probe_image();
if (!probe) {
return runtime_test_fixture_start_failed(tag, "image-probe", 0);
}
if (!runtime_test_fixture_prepare(&daemon->fixture, tag)) {
return false;
}
const char *argv[] = {probe,
"__cbm_runtime_serve",
daemon->fixture.parent,
daemon->fixture.key,
version,
with_owner ? "1" : "0",
NULL};
daemon->spawned = runtime_test_probe_daemon_spawn(daemon, argv);
if (!daemon->spawned) {
return runtime_test_fixture_start_failed(tag, "image-probe-spawn",
runtime_test_last_os_error());
}
char line[CBM_DAEMON_BUILD_FINGERPRINT_SIZE + 16] = {0};
bool answered = runtime_test_probe_daemon_read_line(daemon, line, sizeof(line));
bool ready = answered && strncmp(line, "ready ", 6) == 0 && runtime_test_is_fingerprint(line + 6);
if (!ready) {
printf(" image probe daemon did not start: tag=%s reply=%s\n", tag,
answered ? line : "(none: the probe exited)");
return false;
}
memcpy(daemon->build, line + 6, CBM_DAEMON_BUILD_FINGERPRINT_SIZE);
return true;
}
/* Ask the daemon one question ('i' owner interrupted, 'x' exited by itself):
* true when it answered, the answer in *answer. */
static bool runtime_test_probe_daemon_ask(runtime_test_probe_daemon_t *daemon, char command,
bool *answer) {
char line[8] = {0};
bool answered = daemon->spawned && runtime_test_probe_daemon_send(daemon, command) &&
runtime_test_probe_daemon_read_line(daemon, line, sizeof(line)) &&
(strcmp(line, "0") == 0 || strcmp(line, "1") == 0);
*answer = answered && line[0] == '1';
return answered;
}
/* Release the daemon (EOF on its stdin), reap it and remove the fixture's
* paths. Returns its exit code, or -1 when it never ran or did not exit
* normally. Safe on a daemon whose start failed, and on a memset one. */
static int runtime_test_probe_daemon_finish(runtime_test_probe_daemon_t *daemon) {
int exit_code = -1;
if (daemon->spawned) {
#ifdef _WIN32
(void)CloseHandle(daemon->commands);
bool waited = runtime_test_windows_wait_image_probe(daemon->process);
DWORD code = 0;
if (waited && GetExitCodeProcess(daemon->process, &code) != 0 && code <= INT_MAX) {
exit_code = (int)code;
}
(void)CloseHandle(daemon->process);
(void)CloseHandle(daemon->replies);
#elif defined(__APPLE__) || defined(__linux__)
(void)close(daemon->commands);
int status = 0;
pid_t waited;
do {
waited = waitpid(daemon->process, &status, 0);
} while (waited < 0 && errno == EINTR);
if (waited == daemon->process && WIFEXITED(status)) {
exit_code = WEXITSTATUS(status);
}
(void)close(daemon->replies);
#endif
daemon->spawned = false;
}
runtime_test_fixture_finish(&daemon->fixture);
return exit_code;
}
static bool runtime_test_read_log(const char *path, char out[RUNTIME_TEST_LOG_CAP]) {
FILE *file = cbm_fopen(path, "rb");
if (!file) {
@@ -2323,8 +2665,6 @@ TEST(daemon_runtime_activation_ack_snapshots_then_interrupts_all_clients) {
#if defined(_WIN32) || defined(__APPLE__) || defined(__linux__)
TEST(daemon_runtime_activation_accepts_authenticated_different_build) {
cbm_daemon_build_identity_t active_identity =
runtime_test_identity("2.4.0", runtime_test_self_build());
char directory[RUNTIME_TEST_PATH_CAP] = {0};
char foreign_image[RUNTIME_TEST_PATH_CAP] = {0};
int directory_written =
@@ -2334,8 +2674,12 @@ TEST(daemon_runtime_activation_accepts_authenticated_different_build) {
int image_written = directory_created ? snprintf(foreign_image, sizeof(foreign_image),
"%s/foreign-activation", directory)
: -1;
/* The active daemon and the foreign requester are both the image probe
* (runtime_test_probe_daemon_t): the daemon hashes the requester's image
* to authenticate its claimed build, over a few MB instead of a copy of
* this runner. */
bool copied = image_written > 0 && image_written < (int)sizeof(foreign_image) &&
runtime_test_copy_self_image(foreign_image);
runtime_test_copy_probe_image(foreign_image);
#ifdef __APPLE__
/* Appending an overlay invalidates Mach-O strict validation. A distinct
* signing identifier changes the executable bytes while keeping the copy
@@ -2349,38 +2693,28 @@ TEST(daemon_runtime_activation_accepts_authenticated_different_build) {
char foreign_build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE] = {0};
bool fingerprinted =
runnable && cbm_daemon_build_fingerprint_file(foreign_image, foreign_build);
bool differs = fingerprinted && strcmp(foreign_build, active_identity.build_fingerprint) != 0;
cbm_daemon_build_identity_t foreign_identity = runtime_test_identity("9.9.9", foreign_build);
/* Finalize the foreign executable before the runtime starts any worker
* threads. In particular, macOS code signing is an external process; the
* test must not fork that helper from the live multithreaded daemon. The
* immutable signed copy remains the actual peer image authenticated by
* the activation request below. */
runtime_test_fixture_t fixture;
memset(&fixture, 0, sizeof(fixture));
bool started =
differs && runtime_test_fixture_start(&fixture, "activation-foreign", &active_identity);
cbm_daemon_runtime_connect_result_t owner_result = {0};
cbm_daemon_runtime_client_t *owner = NULL;
if (started) {
owner = cbm_daemon_runtime_client_connect(fixture.endpoint, &active_identity,
RUNTIME_TEST_TIMEOUT_MS, &owner_result);
}
/* The foreign executable is final before the daemon starts; macOS code
* signing above is an external process. The immutable signed copy remains
* the actual peer image authenticated by the activation request below.
* The daemon holds one committed owner session of its own image. */
runtime_test_probe_daemon_t daemon;
memset(&daemon, 0, sizeof(daemon));
bool started = fingerprinted &&
runtime_test_probe_daemon_start(&daemon, "activation-foreign", "2.4.0", true);
bool differs = started && strcmp(foreign_build, daemon.build) != 0;
int foreign_exit = -1;
bool foreign_ran =
owner && differs &&
runtime_test_run_activation_image(foreign_image, &fixture, &foreign_identity,
differs &&
runtime_test_run_activation_image(foreign_image, &daemon.fixture, &foreign_identity,
CBM_DAEMON_RUNTIME_ACTIVATION_INSTALL, &foreign_exit);
bool owner_interrupted = owner && foreign_ran &&
!cbm_daemon_runtime_client_heartbeat(owner, RUNTIME_TEST_TIMEOUT_MS);
if (owner) {
(void)cbm_daemon_runtime_client_close(owner, RUNTIME_TEST_TIMEOUT_MS);
owner = NULL;
}
bool exited =
started && cbm_daemon_runtime_service_wait_exited(fixture.service, RUNTIME_TEST_TIMEOUT_MS);
runtime_test_fixture_finish(&fixture);
bool owner_interrupted = false;
bool interrupted_asked =
foreign_ran && runtime_test_probe_daemon_ask(&daemon, 'i', &owner_interrupted);
bool exited = false;
bool exited_asked = started && runtime_test_probe_daemon_ask(&daemon, 'x', &exited);
int daemon_exit = runtime_test_probe_daemon_finish(&daemon);
if (image_written > 0 && image_written < (int)sizeof(foreign_image)) {
(void)cbm_unlink(foreign_image);
}
@@ -2388,18 +2722,20 @@ TEST(daemon_runtime_activation_accepts_authenticated_different_build) {
(void)cbm_rmdir(directory);
}
ASSERT_TRUE(started);
ASSERT_EQ(owner_result.status, CBM_DAEMON_RUNTIME_CONNECT_ACCEPTED);
ASSERT_TRUE(directory_created);
ASSERT_TRUE(copied);
ASSERT_TRUE(changed);
ASSERT_TRUE(runnable);
ASSERT_TRUE(fingerprinted);
ASSERT_TRUE(started);
ASSERT_TRUE(differs);
ASSERT_TRUE(foreign_ran);
ASSERT_EQ(foreign_exit, 0);
ASSERT_TRUE(interrupted_asked);
ASSERT_TRUE(owner_interrupted);
ASSERT_TRUE(exited_asked);
ASSERT_TRUE(exited);
ASSERT_EQ(daemon_exit, 0);
PASS();
}
#endif
@@ -4875,15 +5211,20 @@ TEST(daemon_runtime_process_fingerprint_supports_extended_length_image) {
}
bool directory_created = path_built && cbm_mkdir_p(directory, 0700);
int image_written = directory_created ? snprintf(image_path, sizeof(image_path),
"%s/test-runner-long-image.exe", directory)
"%s/image-probe-long-image.exe", directory)
: -1;
wchar_t *ordinary_wide = image_written > 0 && image_written < (int)sizeof(image_path)
? cbm_utf8_to_wide(image_path)
: NULL;
bool exceeds_legacy_limit = ordinary_wide && wcslen(ordinary_wide) >= MAX_PATH;
free(ordinary_wide);
bool copied = exceeds_legacy_limit && runtime_test_windows_copy_self(image_path);
const char *expected = runtime_test_self_build();
/* A copy of the image probe, not of this runner: the fingerprint below
* hashes the whole held image (runtime_test_probe_daemon_t says why). */
bool copied = exceeds_legacy_limit && runtime_test_copy_probe_image(image_path);
char expected_build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE] = {0};
const char *probe = runtime_test_probe_image();
const char *expected =
probe && cbm_daemon_build_fingerprint_file(probe, expected_build) ? expected_build : NULL;
int event_written =
snprintf(event_name, sizeof(event_name), "Local\\cbm-runtime-long-image-%lu-%llu",
@@ -4964,10 +5305,7 @@ TEST(daemon_runtime_process_fingerprint_supports_extended_length_image) {
* Re-signing the changed copy keeps the probe runnable on arm64 while ensuring
* that only the deliberately injected mapping has the active identity. */
TEST(daemon_runtime_mac_fast_path_rejects_foreign_main_image_mapping_active) {
cbm_daemon_build_identity_t identity =
runtime_test_identity("2.4.0", runtime_test_self_build());
char directory[RUNTIME_TEST_PATH_CAP] = {0};
char active_image[RUNTIME_TEST_PATH_CAP] = {0};
char foreign_image[RUNTIME_TEST_PATH_CAP] = {0};
char foreign_fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE] = {0};
int directory_written =
@@ -4977,7 +5315,11 @@ TEST(daemon_runtime_mac_fast_path_rejects_foreign_main_image_mapping_active) {
int foreign_written = directory_created ? snprintf(foreign_image, sizeof(foreign_image),
"%s/foreign-client", directory)
: -1;
bool active_resolved = runtime_test_self_image_path(active_image);
/* The daemon runs from the image probe (runtime_test_probe_daemon_t), so
* the active image the foreign client maps is the probe, and the foreign
* main image the daemon must hash is a re-signed copy of the probe. */
const char *active_image = runtime_test_probe_image();
bool active_resolved = active_image != NULL;
bool foreign_copied = active_resolved && foreign_written > 0 &&
foreign_written < (int)sizeof(foreign_image) &&
runtime_test_copy_executable(active_image, foreign_image);
@@ -4987,22 +5329,20 @@ TEST(daemon_runtime_mac_fast_path_rejects_foreign_main_image_mapping_active) {
bool foreign_signed = foreign_changed && runtime_test_mac_ad_hoc_sign(foreign_image);
bool foreign_fingerprinted =
foreign_signed && cbm_daemon_build_fingerprint_file(foreign_image, foreign_fingerprint);
bool fingerprint_differs =
foreign_fingerprinted && strcmp(foreign_fingerprint, identity.build_fingerprint) != 0;
runtime_test_fixture_t fixture;
memset(&fixture, 0, sizeof(fixture));
bool fixture_attempted = directory_created && active_resolved && fingerprint_differs;
bool started =
fixture_attempted && runtime_test_fixture_start(&fixture, "mapped-foreign", &identity);
runtime_test_probe_daemon_t daemon;
memset(&daemon, 0, sizeof(daemon));
bool started = directory_created && foreign_fingerprinted &&
runtime_test_probe_daemon_start(&daemon, "mapped-foreign", "2.4.0", false);
cbm_daemon_build_identity_t identity = runtime_test_identity("2.4.0", daemon.build);
bool fingerprint_differs =
started && strcmp(foreign_fingerprint, identity.build_fingerprint) != 0;
int foreign_exit = -1;
bool foreign_ran =
started && runtime_test_run_mapped_hello_image(foreign_image, active_image, &fixture,
&identity, &foreign_exit);
if (fixture_attempted) {
runtime_test_fixture_finish(&fixture);
}
fingerprint_differs && runtime_test_run_mapped_hello_image(
foreign_image, active_image, &daemon.fixture, &identity,
&foreign_exit);
int daemon_exit = runtime_test_probe_daemon_finish(&daemon);
if (foreign_written > 0 && foreign_written < (int)sizeof(foreign_image)) {
(void)cbm_unlink(foreign_image);
}
@@ -5016,33 +5356,41 @@ TEST(daemon_runtime_mac_fast_path_rejects_foreign_main_image_mapping_active) {
ASSERT_TRUE(foreign_changed);
ASSERT_TRUE(foreign_signed);
ASSERT_TRUE(foreign_fingerprinted);
ASSERT_TRUE(fingerprint_differs);
ASSERT_TRUE(started);
ASSERT_TRUE(fingerprint_differs);
ASSERT_TRUE(foreign_ran);
/* 26 = the daemon answered with a rejection. A probe that got no answer
* inside the exchange budget exits 30 and must not pass here. */
ASSERT_EQ(foreign_exit, 26);
ASSERT_EQ(daemon_exit, 0);
PASS();
}
#endif
/* The daemon admits a peer running a DIFFERENT file only after hashing that
* file: identical bytes are admitted, changed bytes refused. The daemon runs
* from the image probe and its peers are copies of the probe
* (runtime_test_probe_daemon_t), so the hash it pays before answering is over
* a few MB, not over a copy of this runner. */
TEST(daemon_runtime_copied_image_fallback_accepts_identical_and_rejects_changed) {
cbm_daemon_build_identity_t identity =
runtime_test_identity("2.4.0", runtime_test_self_build());
runtime_test_fixture_t identical_fixture;
runtime_test_probe_daemon_t identical_daemon;
bool identical_started =
runtime_test_fixture_start(&identical_fixture, "copied-identical", &identity);
runtime_test_probe_daemon_start(&identical_daemon, "copied-identical", "2.4.0", false);
cbm_daemon_build_identity_t identity = runtime_test_identity("2.4.0", identical_daemon.build);
char identical_path[RUNTIME_TEST_PATH_CAP] = {0};
int identical_path_written = identical_started
#ifdef _WIN32
? snprintf(identical_path, sizeof(identical_path),
"%s/client-copy.exe", identical_fixture.parent)
"%s/client-copy.exe",
identical_daemon.fixture.parent)
#else
? snprintf(identical_path, sizeof(identical_path),
"%s/client-copy", identical_fixture.parent)
"%s/client-copy", identical_daemon.fixture.parent)
#endif
: -1;
bool identical_copied = identical_path_written > 0 &&
identical_path_written < (int)sizeof(identical_path) &&
runtime_test_copy_self_image(identical_path);
runtime_test_copy_probe_image(identical_path);
char identical_fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE] = {0};
bool identical_bytes =
identical_copied &&
@@ -5050,35 +5398,39 @@ TEST(daemon_runtime_copied_image_fallback_accepts_identical_and_rejects_changed)
strcmp(identical_fingerprint, identity.build_fingerprint) == 0;
int identical_exit = -1;
bool identical_ran =
identical_bytes && runtime_test_run_hello_image(identical_path, &identical_fixture,
identical_bytes && runtime_test_run_hello_image(identical_path, &identical_daemon.fixture,
&identity, &identical_exit);
bool identical_exited =
identical_ran && identical_exit == 0 &&
cbm_daemon_runtime_service_wait_exited(identical_fixture.service, RUNTIME_TEST_TIMEOUT_MS);
bool identical_exited = false;
bool identical_asked = identical_ran && identical_exit == 0 &&
runtime_test_probe_daemon_ask(&identical_daemon, 'x', &identical_exited);
(void)cbm_unlink(identical_path);
runtime_test_fixture_finish(&identical_fixture);
int identical_daemon_exit = runtime_test_probe_daemon_finish(&identical_daemon);
#ifdef __linux__
runtime_test_fixture_t changed_fixture;
runtime_test_probe_daemon_t changed_daemon;
bool changed_started =
runtime_test_fixture_start(&changed_fixture, "copied-changed", &identity);
runtime_test_probe_daemon_start(&changed_daemon, "copied-changed", "2.4.0", false);
cbm_daemon_build_identity_t changed_identity =
runtime_test_identity("2.4.0", changed_daemon.build);
char changed_path[RUNTIME_TEST_PATH_CAP] = {0};
int changed_path_written = changed_started ? snprintf(changed_path, sizeof(changed_path),
"%s/client-copy", changed_fixture.parent)
: -1;
int changed_path_written =
changed_started ? snprintf(changed_path, sizeof(changed_path), "%s/client-copy",
changed_daemon.fixture.parent)
: -1;
bool changed_copied = changed_path_written > 0 &&
changed_path_written < (int)sizeof(changed_path) &&
runtime_test_copy_self_image(changed_path) &&
runtime_test_copy_probe_image(changed_path) &&
runtime_test_append_image_marker(changed_path);
char changed_fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE] = {0};
bool changed_bytes = changed_copied &&
cbm_daemon_build_fingerprint_file(changed_path, changed_fingerprint) &&
strcmp(changed_fingerprint, identity.build_fingerprint) != 0;
strcmp(changed_fingerprint, changed_identity.build_fingerprint) != 0;
int changed_exit = -1;
bool changed_ran = changed_bytes && runtime_test_run_hello_image(changed_path, &changed_fixture,
&identity, &changed_exit);
bool changed_ran =
changed_bytes && runtime_test_run_hello_image(changed_path, &changed_daemon.fixture,
&changed_identity, &changed_exit);
(void)cbm_unlink(changed_path);
runtime_test_fixture_finish(&changed_fixture);
int changed_daemon_exit = runtime_test_probe_daemon_finish(&changed_daemon);
#endif
ASSERT_TRUE(identical_started);
@@ -5086,13 +5438,18 @@ TEST(daemon_runtime_copied_image_fallback_accepts_identical_and_rejects_changed)
ASSERT_TRUE(identical_bytes);
ASSERT_TRUE(identical_ran);
ASSERT_EQ(identical_exit, 0);
ASSERT_TRUE(identical_asked);
ASSERT_TRUE(identical_exited);
ASSERT_EQ(identical_daemon_exit, 0);
#ifdef __linux__
ASSERT_TRUE(changed_started);
ASSERT_TRUE(changed_copied);
ASSERT_TRUE(changed_bytes);
ASSERT_TRUE(changed_ran);
/* 26 = the daemon answered with a rejection; no answer inside the
* exchange budget exits 30 and fails here instead of passing. */
ASSERT_EQ(changed_exit, 26);
ASSERT_EQ(changed_daemon_exit, 0);
#endif
PASS();
}
@@ -5118,7 +5475,7 @@ TEST(daemon_runtime_process_fingerprint_never_hashes_replacement_path) {
setup = setup && image_written > 0 && image_written < (int)sizeof(image_path) &&
replacement_written > 0 && replacement_written < (int)sizeof(replacement_path) &&
event_written > 0 && event_written < (int)sizeof(event_name) &&
runtime_test_windows_copy_self(image_path);
runtime_test_copy_probe_image(image_path);
FILE *replacement_file = setup ? cbm_fopen(replacement_path, "wb") : NULL;
bool replacement_written_ok =
@@ -5188,12 +5545,13 @@ TEST(daemon_runtime_process_fingerprint_never_hashes_replacement_path) {
int replacement_written =
setup ? snprintf(replacement_path, sizeof(replacement_path), "%s/replacement", directory)
: -1;
/* The copied image is this runner in holder mode, not a system utility:
* a multi-call coreutils /bin/cat (uutils) prints "unknown program" and
* exits when executed under the copied name. */
/* The copied image is the image probe in holder mode, not a system
* utility: a multi-call coreutils /bin/cat (uutils) prints "unknown
* program" and exits when executed under the copied name. Not this runner
* either: the fingerprint below hashes the whole held image. */
setup = setup && image_written > 0 && image_written < (int)sizeof(image_path) &&
replacement_written > 0 && replacement_written < (int)sizeof(replacement_path) &&
runtime_test_copy_self_image(image_path);
runtime_test_copy_probe_image(image_path);
FILE *replacement_file = setup ? cbm_fopen(replacement_path, "wb") : NULL;
bool replacement_written_ok =
+289
View File
@@ -1273,6 +1273,292 @@ TEST(discover_deploy_excluded_full_mode) {
PASS();
}
typedef struct {
const char *path;
const char *content;
CBMLanguage language; /* the language discovery gives it; CBM_LANG_COUNT: not discovered */
} discover_case_t;
/* The language discovery gave rel_path, or CBM_LANG_COUNT when it is absent. */
static CBMLanguage discover_language_of(const cbm_file_info_t *files, int count,
const char *rel_path) {
for (int i = 0; i < count; i++) {
if (strcmp(files[i].rel_path, rel_path) == 0) {
return files[i].language;
}
}
return CBM_LANG_COUNT;
}
/* Writes the cases under base, discovers in full mode and checks each case's
* language (CBM_LANG_COUNT: not discovered). Returns the number of mismatches,
* each printed, or -1 when discovery fails. */
static int discover_check_cases(const char *base, const discover_case_t *cases, size_t n) {
for (size_t i = 0; i < n; i++) {
th_write_file(TH_PATH(base, cases[i].path), cases[i].content);
}
cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL};
cbm_file_info_t *files = NULL;
int count = 0;
if (cbm_discover(base, &opts, &files, &count) != 0) {
return -1;
}
int mismatches = 0;
for (size_t i = 0; i < n; i++) {
CBMLanguage got = discover_language_of(files, count, cases[i].path);
if (got != cases[i].language) {
fprintf(stderr, " %s: language %d, expected %d\n", cases[i].path, (int)got,
(int)cases[i].language);
mismatches++;
}
}
cbm_discover_free(files, count);
return mismatches;
}
/* Inside deploy/ and deployed/ discovery indexes deployment and configuration
* files, one of each family here, and keeps skipping program source: build
* copies of source there (discover_deploy_excluded_full_mode) still make no
* duplicate classes. A file is admitted by its language, never by its
* folder: a .py beside a chart stays out. Below a JVM source root `deploy` is
* a package like any other and is indexed in full, as before. */
TEST(discover_deploy_indexes_config_files_only) {
static const discover_case_t cases[] = {
{"deploy/k8s/deployment.yaml", "apiVersion: apps/v1\nkind: Deployment\n", CBM_LANG_YAML},
{"deploy/k8s/service.json", "{\"apiVersion\": \"v1\", \"kind\": \"Service\"}\n",
CBM_LANG_JSON},
{"deploy/overlays/prod/kustomization.yaml", "resources:\n - ../../k8s\n",
CBM_LANG_KUSTOMIZE},
{"deploy/chart/Chart.yaml", "apiVersion: v2\nname: web\nversion: 0.1.0\n", CBM_LANG_YAML},
{"deploy/chart/templates/deployment.yaml", "kind: Deployment\n", CBM_LANG_YAML},
{"deploy/chart/templates/_helpers.tpl", "{{- define \"web.name\" -}}web{{- end }}\n",
CBM_LANG_GOTEMPLATE},
{"deploy/terraform/main.tf", "resource \"null_resource\" \"x\" {}\n", CBM_LANG_HCL},
{"deploy/packer/image.pkr.hcl", "source \"null\" \"x\" {}\n", CBM_LANG_HCL},
{"deploy/Dockerfile", "FROM alpine:3.20\n", CBM_LANG_DOCKERFILE},
{"deploy/docker-compose.yml", "services:\n web:\n image: alpine\n", CBM_LANG_YAML},
{"deploy/install.sh", "kubectl apply -f k8s\n", CBM_LANG_BASH},
{"deploy/bootstrap", "#!/bin/bash\necho up\n", CBM_LANG_BASH},
{"deploy/prod.env", "MODE=prod\n", CBM_LANG_DOTENV},
{"deployed/stack/main.tf", "variable \"x\" {}\n", CBM_LANG_HCL},
{"deployed/.env.production", "MODE=prod\n", CBM_LANG_DOTENV},
/* the registry's build and configuration languages */
{"deploy/Makefile", "deploy:\n\tkubectl apply -f k8s\n", CBM_LANG_MAKEFILE},
{"deploy/cmake/CMakeLists.txt", "project(web)\n", CBM_LANG_CMAKE},
{"deploy/config.toml", "[server]\nport = 8080\n", CBM_LANG_TOML},
{"deploy/app.ini", "[app]\nmode = prod\n", CBM_LANG_INI},
{"deploy/nginx/nginx.conf", "worker_processes = 2\n", CBM_LANG_INI},
/* the other configuration and infrastructure languages */
{"deploy/app.properties", "server.port=8080\n", CBM_LANG_PROPERTIES},
{"deploy/web.xml", "<web-app></web-app>\n", CBM_LANG_XML},
{"deploy/settings.json5", "{port: 8080}\n", CBM_LANG_JSON5},
{"deploy/stack.jsonnet", "{ port: 8080 }\n", CBM_LANG_JSONNET},
{"deploy/templates/app.conf.j2", "port = {{ port }}\n", CBM_LANG_JINJA2},
{"deploy/main.bicep", "param location string\n", CBM_LANG_BICEP},
{"deploy/shell.nix", "{ pkgs }: pkgs.mkShell {}\n", CBM_LANG_NIX},
{"deploy/site.pp", "node default {}\n", CBM_LANG_PUPPET},
{"deploy/config.pkl", "port = 8080\n", CBM_LANG_PKL},
/* the rest of the shell family */
{"deploy/run.ps1", "Write-Host up\n", CBM_LANG_POWERSHELL},
{"deploy/setup.fish", "echo up\n", CBM_LANG_FISH},
/* dropped: SQL, Groovy, and the dependency manifests the package-map
* walk skips there (by name, whatever their language) */
{"deploy/schema.sql", "CREATE TABLE users (id int);\n", CBM_LANG_COUNT},
{"deploy/pipeline.groovy", "def run() {}\n", CBM_LANG_COUNT},
{"deploy/requirements.txt", "ansible==9.0\n", CBM_LANG_COUNT},
{"deploy/tool/go.mod", "module example.com/tool\n", CBM_LANG_COUNT},
{"deploy/tool/Cargo.toml", "[package]\nname = \"tool\"\n", CBM_LANG_COUNT},
{"deploy/tool/pyproject.toml", "[project]\nname = \"tool\"\n", CBM_LANG_COUNT},
{"deploy/tool/pom.xml", "<project></project>\n", CBM_LANG_COUNT},
{"deploy/tool/pubspec.yaml", "name: tool\n", CBM_LANG_COUNT},
/* the same names outside: indexed, so the rule is what drops them */
{"ops/schema.sql", "CREATE TABLE users (id int);\n", CBM_LANG_SQL},
{"ops/Cargo.toml", "[package]\nname = \"ops\"\n", CBM_LANG_TOML},
{"ops/pom.xml", "<project></project>\n", CBM_LANG_XML},
/* program source stays out, wherever it sits below the directory; each
* has a configuration file beside it, so its directory is walked */
{"deploy/main.go", "package deploy\n", CBM_LANG_COUNT},
{"deploy/chart/plugin.py", "def hook():\n pass\n", CBM_LANG_COUNT},
{"deploy/lib/App.java", "class App {}\n", CBM_LANG_COUNT},
{"deploy/lib/replicas.json", "{\"replicas\": 2}\n", CBM_LANG_JSON},
{"deploy/run", "#!/usr/bin/env python3\nprint(1)\n", CBM_LANG_COUNT},
{"deployed/web/index.ts", "export class Web {}\n", CBM_LANG_COUNT},
{"deployed/web/Dockerfile", "FROM nginx:1.27\n", CBM_LANG_DOCKERFILE},
/* the same program files outside: indexed, so the rule is what drops them */
{"src/main.go", "package main\n", CBM_LANG_GO},
{"src/plugin.py", "def hook():\n pass\n", CBM_LANG_PYTHON},
{"src/App.java", "class App {}\n", CBM_LANG_JAVA},
{"src/run", "#!/usr/bin/env python3\nprint(1)\n", CBM_LANG_PYTHON},
{"web/index.ts", "export class Web {}\n", CBM_LANG_TYPESCRIPT},
{"src/main/java/com/acme/deploy/Tool.java", "package com.acme.deploy;\nclass Tool {}\n",
CBM_LANG_JAVA},
};
char *base = th_mktempdir("cbm_disc_deploy_cfg");
ASSERT(base != NULL);
int mismatches = discover_check_cases(base, cases, sizeof(cases) / sizeof(cases[0]));
/* The walk reached each dropped file and recorded why (#963), as it
* records every built-in skip-list drop. */
cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL};
cbm_file_info_t *files = NULL;
int count = 0;
char **excluded = NULL;
int excluded_count = 0;
cbm_ignored_file_t *ignored = NULL;
int ignored_count = 0;
int ignored_total = 0;
int rc = cbm_discover_ex2(base, &opts, &files, &count, &excluded, &excluded_count, &ignored,
&ignored_count, &ignored_total);
static const char *const dropped[] = {"deploy/main.go",
"deploy/chart/plugin.py",
"deploy/lib/App.java",
"deploy/run",
"deployed/web/index.ts",
"deploy/schema.sql",
"deploy/pipeline.groovy",
"deploy/requirements.txt",
"deploy/tool/go.mod",
"deploy/tool/Cargo.toml",
"deploy/tool/pyproject.toml",
"deploy/tool/pom.xml",
"deploy/tool/pubspec.yaml"};
int recorded = 0;
for (size_t d = 0; d < sizeof(dropped) / sizeof(dropped[0]); d++) {
for (int i = 0; i < ignored_count; i++) {
recorded += strcmp(ignored[i].rel_path, dropped[d]) == 0 &&
strcmp(ignored[i].reason, "skip-list") == 0;
}
}
int deploy_excluded = 0;
for (int i = 0; i < excluded_count; i++) {
deploy_excluded += strncmp(excluded[i], "deploy", strlen("deploy")) == 0;
}
cbm_discover_free(files, count);
cbm_discover_free_excluded(excluded, excluded_count);
cbm_discover_free_ignored(ignored, ignored_count);
th_cleanup(base);
ASSERT_EQ(mismatches, 0);
ASSERT_EQ(rc, 0);
ASSERT_EQ(recorded, (int)(sizeof(dropped) / sizeof(dropped[0])));
ASSERT_EQ(deploy_excluded, 0); /* no deploy or deployed subtree is excluded any more */
PASS();
}
/* A directory named env is skipped only when it is a Python virtual
* environment: python -m venv writes pyvenv.cfg, virtualenv an activate
* script in bin/ (POSIX) or Scripts/ (Windows). Any other env directory, a Go
* package or a folder of settings, is walked like any other. */
TEST(discover_env_skipped_only_as_virtualenv) {
static const discover_case_t cases[] = {
{"a/env/pyvenv.cfg", "home = /usr/bin\n", CBM_LANG_COUNT},
{"a/env/lib/tool.py", "def tool():\n pass\n", CBM_LANG_COUNT},
{"b/env/bin/activate", "# This file must be used with source\n", CBM_LANG_COUNT},
{"b/env/bin/tool.py", "def tool():\n pass\n", CBM_LANG_COUNT},
{"c/env/Scripts/activate", "# This file must be used with source\n", CBM_LANG_COUNT},
{"c/env/Scripts/tool.py", "def tool():\n pass\n", CBM_LANG_COUNT},
/* conda writes conda-meta/history into every environment, and no
* pyvenv.cfg or activate script into a named one */
{"d/env/conda-meta/history", "==> 2026-11-18 <==\n", CBM_LANG_COUNT},
{"d/env/lib/python3.12/os.py", "def getenv(key):\n pass\n", CBM_LANG_COUNT},
/* beside each virtual environment: walked */
{"a/tool.py", "def tool():\n pass\n", CBM_LANG_PYTHON},
{"b/tool.py", "def tool():\n pass\n", CBM_LANG_PYTHON},
{"c/tool.py", "def tool():\n pass\n", CBM_LANG_PYTHON},
{"d/tool.py", "def tool():\n pass\n", CBM_LANG_PYTHON},
/* a marker is its exact stored name: one that differs in letter case
* makes no virtual environment, on any platform */
{"e/env/PYVENV.CFG", "home = /usr/bin\n", CBM_LANG_COUNT},
{"e/env/e.py", "x = 1\n", CBM_LANG_PYTHON},
{"f/env/scripts/activate", "# activate\n", CBM_LANG_COUNT},
{"f/env/f.py", "x = 1\n", CBM_LANG_PYTHON},
{"g/env/bin/Activate", "# activate\n", CBM_LANG_COUNT},
{"g/env/g.py", "x = 1\n", CBM_LANG_PYTHON},
{"h/env/Bin/activate", "# activate\n", CBM_LANG_COUNT},
{"h/env/h.py", "x = 1\n", CBM_LANG_PYTHON},
{"pkg/util/env/env.go", "package env\n", CBM_LANG_GO},
{"config/env/app.yaml", "mode: prod\n", CBM_LANG_YAML},
{"env/settings.py", "DEBUG = False\n", CBM_LANG_PYTHON},
/* a marker name in a directory that is not env proves nothing */
{"tools/bin/activate.py", "def activate():\n pass\n", CBM_LANG_PYTHON},
};
char *base = th_mktempdir("cbm_disc_env_venv");
ASSERT(base != NULL);
int mismatches = discover_check_cases(base, cases, sizeof(cases) / sizeof(cases[0]));
cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL};
cbm_file_info_t *files = NULL;
int count = 0;
char **excluded = NULL;
int excluded_count = 0;
int rc = cbm_discover_ex(base, &opts, &files, &count, &excluded, &excluded_count);
int venvs_reported = 0;
int env_excluded = 0;
for (int i = 0; i < excluded_count; i++) {
venvs_reported += strcmp(excluded[i], "a/env") == 0 || strcmp(excluded[i], "b/env") == 0 ||
strcmp(excluded[i], "c/env") == 0 || strcmp(excluded[i], "d/env") == 0;
size_t len = strlen(excluded[i]);
env_excluded +=
len >= strlen("/env") && strcmp(excluded[i] + len - strlen("/env"), "/env") == 0;
}
cbm_discover_free(files, count);
cbm_discover_free_excluded(excluded, excluded_count);
th_cleanup(base);
ASSERT_EQ(mismatches, 0);
ASSERT_EQ(rc, 0);
ASSERT_EQ(venvs_reported, 4); /* each virtual environment is an excluded subtree */
ASSERT_EQ(env_excluded, 4); /* and no other env directory is */
PASS();
}
/* The virtual-environment test only looks at entries inside the repository:
* a marker reached through a symlink, or a marker that is itself a symlink,
* proves nothing, as discovery never follows either. */
TEST(discover_env_venv_marker_through_symlink_not_followed) {
#ifdef _WIN32
SKIP_PLATFORM("Windows: symlinks need admin / symlink() unavailable");
#else
/* th_mktempdir returns one static buffer: keep the first path. */
char outside[256];
const char *made = th_mktempdir("cbm_disc_env_outside");
ASSERT(made != NULL);
snprintf(outside, sizeof(outside), "%s", made);
th_write_file(TH_PATH(outside, "bin/activate"), "# activate\n");
th_write_file(TH_PATH(outside, "pyvenv.cfg"), "home = /usr/bin\n");
char *base = th_mktempdir("cbm_disc_env_link");
ASSERT(base != NULL);
th_write_file(TH_PATH(base, "d/env/env.go"), "package env\n");
th_write_file(TH_PATH(base, "e/env/env.go"), "package env\n");
/* the same layout with the marker inside the repository is a venv */
th_write_file(TH_PATH(base, "f/env/bin/activate"), "# activate\n");
th_write_file(TH_PATH(base, "f/env/env.go"), "package env\n");
char target[512];
char link[512];
snprintf(target, sizeof(target), "%s/bin", outside);
snprintf(link, sizeof(link), "%s/d/env/bin", base);
int linked_dir = symlink(target, link);
snprintf(target, sizeof(target), "%s/pyvenv.cfg", outside);
snprintf(link, sizeof(link), "%s/e/env/pyvenv.cfg", base);
int linked_file = symlink(target, link);
cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL};
cbm_file_info_t *files = NULL;
int count = 0;
int rc = cbm_discover(base, &opts, &files, &count);
bool has_d = rc == 0 && discover_has_rel_path(files, count, "d/env/env.go");
bool has_e = rc == 0 && discover_has_rel_path(files, count, "e/env/env.go");
bool has_f = rc == 0 && discover_has_rel_path(files, count, "f/env/env.go");
cbm_discover_free(files, count);
th_cleanup(base);
th_cleanup(outside);
ASSERT_EQ(linked_dir, 0);
ASSERT_EQ(linked_file, 0);
ASSERT_EQ(rc, 0);
ASSERT_TRUE(has_d);
ASSERT_TRUE(has_e);
ASSERT_FALSE(has_f);
PASS();
#endif
}
TEST(discover_cbmignore_no_git) {
char *base = th_mktempdir("cbm_disc_nogit");
ASSERT(base != NULL);
@@ -2621,6 +2907,9 @@ SUITE(discover) {
RUN_TEST(discover_generic_dirs_full_mode);
RUN_TEST(discover_generic_dirs_fast_mode);
RUN_TEST(discover_deploy_excluded_full_mode);
RUN_TEST(discover_deploy_indexes_config_files_only);
RUN_TEST(discover_env_skipped_only_as_virtualenv);
RUN_TEST(discover_env_venv_marker_through_symlink_not_followed);
RUN_TEST(discover_cbmignore_no_git);
/* .cbmignore negation vs built-in skip dirs (issue #500) */
+38 -7
View File
@@ -543,7 +543,7 @@ static const char *jt_reason(const char *db, const char *rel, const char *raw) {
"package a;\n" /* 1 */ \
"\n" /* 2 */ \
"import b.Helper;\n" /* 3 */ \
"import a.env.Config;\n" /* 4 */ \
"import a.tmp.Config;\n" /* 4 */ \
"import java.util.List;\n" /* 5 */ \
"import b.Generated;\n" /* 6 */ \
"/**\n" /* 7 */ \
@@ -551,7 +551,7 @@ static const char *jt_reason(const char *db, const char *rel, const char *raw) {
*/ \
" * {@value Helper#MASK} {@link List} {@link String} {@link T} {@link Gadget#spin}\n" /* 9 */ \
" * {@link NoSuchType} {@link Gadget#nope} {@link GadgetTests} {@link Config}\n" /* 10 */ \
" * {@link a.env.Other} {@link b} {@link Shapes.Point#x} {@link Shapes.Shape#SIDES}\n" /* 11 \
" * {@link a.tmp.Other} {@link b} {@link Shapes.Point#x} {@link Shapes.Shape#SIDES}\n" /* 11 \
*/ \
" * {@link Shapes.Marker#value()} {@link Shapes.Sized#size} {@link Shapes.Sized#size()}\n" /* 12 */ \
" * {@link Gadget#Gadget()} {@link Shapes.Made#Made(int)} {@link Shapes.Made#Made(String)}\n" /* 13 */ \
@@ -581,7 +581,7 @@ TEST(doc_mentions_jvm_java_families_and_reasons) {
/* a real package in a layout discovery still skips by name: a plain
* src/<pkg>/ tree (the open part of #2661); below src/<set>/<lang>/ the
* same package is kept since #2724 */
jt_write(&r, "src/a/env/Config.java", "package a.env;\npublic class Config {}\n");
jt_write(&r, "src/a/tmp/Config.java", "package a.tmp;\npublic class Config {}\n");
jt_write(&r, "src/test/java/a/GadgetTests.java",
"package a;\n/** Tests {@link Gadget}. */\npublic class GadgetTests {}\n");
ASSERT_EQ(dm_index(r.repo, r.db, NULL), 0);
@@ -648,7 +648,7 @@ TEST(doc_mentions_jvm_java_families_and_reasons) {
/* not_indexed: a package of the repository in a layout discovery still
* skips by name, named by an import and by its qualified name */
ASSERT_STR_EQ(jt_reason(db, w, "Config"), "not_indexed");
ASSERT_STR_EQ(jt_reason(db, w, "a.env.Other"), "not_indexed");
ASSERT_STR_EQ(jt_reason(db, w, "a.tmp.Other"), "not_indexed");
/* graph_gap: a package (no node), a record component, an interface
* constant and an annotation element (declared, no definition), a
* constructor the class has without declaring it */
@@ -682,6 +682,36 @@ TEST(doc_mentions_jvm_java_families_and_reasons) {
/* An oversized reference is unparseable in both languages: the resolver
* never resolves the prefix that was kept. */
/* deploy/ outside a source root keeps its configuration files only, so the
* Java declared there is on disk but not in the graph and a reference to it
* reads not_indexed, as when the whole directory was skipped. env is skipped
* only as a Python virtual environment now: its package binds. */
TEST(doc_mentions_jvm_deploy_package_not_indexed) {
jt_repo_t r;
ASSERT(jt_begin(&r, "jdeploy"));
jt_write(&r, "src/main/java/a/Widget.java",
"package a;\n\nimport a.deploy.Job;\nimport a.env.Settings;\n\n"
"/** {@link Job} {@link Settings} */\npublic class Widget {}\n");
jt_write(&r, "src/a/deploy/Job.java", "package a.deploy;\npublic class Job {}\n");
jt_write(&r, "src/a/deploy/job.yaml", "apiVersion: batch/v1\nkind: Job\n");
jt_write(&r, "src/a/env/Settings.java", "package a.env;\npublic class Settings {}\n");
ASSERT_EQ(dm_index(r.repo, r.db, NULL), 0);
const char *w = "src/main/java/a/Widget.java";
/* the guard first: a deploy package reads not_indexed (on main the whole
* directory was skipped, so main gave the same answer for another reason) */
ASSERT_STR_EQ(jt_reason(r.db, w, "Job"), "not_indexed");
int yaml = dm_count(r.db, "SELECT COUNT(*) FROM nodes WHERE label = 'File' AND "
"file_path = 'src/a/deploy/job.yaml'");
int job =
dm_count(r.db, "SELECT COUNT(*) FROM nodes WHERE file_path = 'src/a/deploy/Job.java'");
ASSERT_EQ(yaml, 1); /* the directory is walked */
ASSERT_EQ(job, 0); /* its Java is not indexed */
ASSERT_STR_EQ(jt_reason(r.db, w, "Settings"), "");
ASSERT_EQ(jt_edge(r.db, "a.Widget", "a.env.Settings"), 1);
jt_end(&r);
PASS();
}
TEST(doc_mentions_jvm_oversized_reference) {
jt_repo_t r;
ASSERT(jt_begin(&r, "big"));
@@ -1099,7 +1129,7 @@ TEST(doc_mentions_jvm_java_member_levels) {
" * [name], [Gadget.turn], [Gadget.both], [Color.RED], [NoSuchThing], [Gadget.nope],\n" /* 8 \
*/ \
" * [GadgetTests], [Gadget.create], [Alias], [Alias.spin], [far.Helper.run], [far.sub],\n" /* 9 */ \
" * [org.outside.Thing], [far.Missing], [k.env.Config]\n" /* 10 */ \
" * [org.outside.Thing], [far.Missing], [k.tmp.Config]\n" /* 10 */ \
" * @see Gadget.spin\n" /* 11 */ \
" * @see NoSuchThing\n" /* 12 */ \
" * @throws CustomError never\n" /* 13 */ \
@@ -1122,7 +1152,7 @@ TEST(doc_mentions_jvm_kotlin_families_and_reasons) {
/* a real package in a layout discovery still skips by name: a plain
* src/<pkg>/ tree (the open part of #2661); below src/<set>/<lang>/ the
* same package is kept since #2724 */
jt_write(&r, "src/k/env/Config.kt", "package k.env\n\nclass Config\n");
jt_write(&r, "src/k/tmp/Config.kt", "package k.tmp\n\nclass Config\n");
jt_write(&r, "src/test/kotlin/k/GadgetTests.kt", "package k\n\nclass GadgetTests\n");
jt_write(&r, "src/test/kotlin/k/samples/Samples.kt",
"package k.samples\n\nfun widgetSample() {}\n");
@@ -1175,7 +1205,7 @@ TEST(doc_mentions_jvm_kotlin_families_and_reasons) {
/* test_only_target */
ASSERT_STR_EQ(jt_reason(db, w, "GadgetTests"), "test_only_target");
/* not_indexed: a package of the repository in a layout discovery still skips by name */
ASSERT_STR_EQ(jt_reason(db, w, "k.env.Config"), "not_indexed");
ASSERT_STR_EQ(jt_reason(db, w, "k.tmp.Config"), "not_indexed");
/* unparseable: a path with an empty name in it */
ASSERT_STR_EQ(jt_reason(db, w, "Gadget..spin"), "unparseable");
/* a `[name]` that names nothing is Markdown text to KDoc: no row. The
@@ -3636,6 +3666,7 @@ SUITE(doc_mentions_jvm) {
RUN_TEST(doc_mentions_jvm_scope_parse_errors);
RUN_TEST(doc_mentions_jvm_resolver_parse_errors);
RUN_TEST(doc_mentions_jvm_java_families_and_reasons);
RUN_TEST(doc_mentions_jvm_deploy_package_not_indexed);
RUN_TEST(doc_mentions_jvm_oversized_reference);
RUN_TEST(doc_mentions_jvm_java_type_levels);
RUN_TEST(doc_mentions_jvm_java_member_levels);
+104
View File
@@ -0,0 +1,104 @@
#ifndef TEST_IMAGE_PROBE_H
#define TEST_IMAGE_PROBE_H
/*
* The image probe: a small executable built beside every test runner
* (Makefile.cbm, <runner>-image-probe, e.g. build/c/test-runner-image-probe)
* from tests/image_probe_main.c, tests/image_probe_roles.c, the daemon runtime
* and foundation (~5 MB with sanitizers, against ~800 MB for the runner).
*
* Tests that need a real, separately executed process image use it instead of
* a copy of the runner: daemon_runtime's copied-image probes run their daemon
* and clients from it, and the cli install tests publish it as the installed
* binary. Each of those used to SHA-256 the whole sanitized runner, several
* times per test, and one such hash costs 70-95 s on some hosted x86 CPUs: a
* daemon still hashing a peer copy missed the client's exchange budget, and the
* cli suite ran past its wall-clock ceiling, decided by which CPU a job drew.
* The production verification code is unchanged; only the image is smaller.
*
* Roles (argv[1]), dispatched by tf_image_probe_dispatch:
* __cbm_runtime_serve <parent> <key> <version> <owner:0|1>
* __cbm_runtime_hello_client <parent> <key> <version> <build>
* __cbm_runtime_activation_client <parent> <key> <version> <build> <action>
* __cbm_runtime_mapped_hello_client <mapped-image> <parent> <key> <version> <build>
* __cbm_runtime_image_holder [<ready-event>]
* The probe's main also answers --version (install verification runs it).
*/
#include "foundation/compat_fs.h" /* cbm_fopen: the probe path may be non-ASCII */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include "foundation/win_utf8.h"
#include <stdlib.h>
#else
#include <unistd.h>
#ifdef __APPLE__
#include <libproc.h>
#endif
#endif
/* Runs the role argv[1] names: its exit code, or -1 when argv names none. */
int tf_image_probe_dispatch(int argc, char **argv);
/* The probe built beside the running runner: the runner's own image path
* without ".exe", then "-image-probe" (".exe" again on Windows). False when
* the path cannot be formed or names no file: a missing probe is a build
* defect the calling test reports, never a skip. */
static inline bool th_image_probe_path(char *out, size_t cap) {
if (!out || cap == 0) {
return false;
}
out[0] = '\0';
char self[4096] = {0};
#ifdef _WIN32
char *module = cbm_module_path_utf8();
size_t module_length = module ? strlen(module) : 0;
bool resolved = module_length > 0 && module_length < sizeof(self);
if (resolved) {
memcpy(self, module, module_length + 1);
}
free(module);
size_t length = resolved ? strlen(self) : 0;
if (length > 4 && _stricmp(self + length - 4, ".exe") == 0) {
self[length - 4] = '\0';
}
const char *suffix = "-image-probe.exe";
#elif defined(__APPLE__)
int length = proc_pidpath(getpid(), self, (uint32_t)sizeof(self));
bool resolved = length > 0 && length < (int)sizeof(self);
if (resolved) {
self[length] = '\0';
}
const char *suffix = "-image-probe";
#elif defined(__linux__)
ssize_t length = readlink("/proc/self/exe", self, sizeof(self) - 1);
bool resolved = length > 0 && length < (ssize_t)sizeof(self) - 1;
if (resolved) {
self[length] = '\0';
}
const char *suffix = "-image-probe";
#else
bool resolved = false;
const char *suffix = "";
#endif
int written = resolved ? snprintf(out, cap, "%s%s", self, suffix) : -1;
if (written <= 0 || (size_t)written >= cap) {
out[0] = '\0';
return false;
}
FILE *probe = cbm_fopen(out, "rb");
if (!probe) {
out[0] = '\0';
return false;
}
(void)fclose(probe);
return true;
}
#endif /* TEST_IMAGE_PROBE_H */
+84
View File
@@ -568,6 +568,89 @@ TEST(index_format_v5_scala_packages_rebuild) {
PASS();
}
/* Format 6 recorded no included files on its lsp_surface rows (#2993). An
* incremental refresh re-extracts the includers of a changed header from that
* record, so a format-6 index refreshed in place would keep an includer's
* edges stale after an edit to its header alone. */
static const RFile k_include_files[] = {
{"lib/m.h", "#ifndef M_H\n#define M_H\n#define DO_WORK(x) real_target(x)\n#endif\n"},
{"lib/a.c",
"#define A_LOCAL 1\n#include \"m.h\"\n\nvoid caller(int v) {\n DO_WORK(v);\n}\n"},
{"lib/t.c", "void real_target(int x) { (void)x; }\nvoid other_target(int x) { (void)x; }\n"},
};
/* CALLS edges from the function `caller` to the Function `target`. */
static int include_caller_calls(cbm_store_t *s, const char *target) {
char sql[512];
snprintf(sql, sizeof(sql),
"SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON "
"t.id=e.target_id WHERE e.type='CALLS' AND s.name='caller' AND t.name='%s' AND "
"t.label='Function'",
target);
sqlite3_stmt *st = NULL;
if (sqlite3_prepare_v2(cbm_store_get_db(s), sql, -1, &st, NULL) != SQLITE_OK) {
return -1;
}
int count = sqlite3_step(st) == SQLITE_ROW ? sqlite3_column_int(st, 0) : -1;
sqlite3_finalize(st);
return count;
}
static int include_rows_recorded(cbm_store_t *s) {
sqlite3_stmt *st = NULL;
if (sqlite3_prepare_v2(cbm_store_get_db(s),
"SELECT COUNT(*) FROM lsp_surface WHERE included_files <> ''", -1, &st,
NULL) != SQLITE_OK) {
return -1;
}
int count = sqlite3_step(st) == SQLITE_ROW ? sqlite3_column_int(st, 0) : -1;
sqlite3_finalize(st);
return count;
}
TEST(index_format_v6_included_files_rebuild) {
RProj lp;
cbm_store_t *q0 = rh_index_files(&lp, k_include_files, 3);
ASSERT_NOT_NULL(q0);
ASSERT_EQ(include_caller_calls(q0, "real_target"), 1);
ASSERT_EQ(include_rows_recorded(q0), 1);
cbm_store_close(q0);
/* The old shape: no row records the files its lookup read. */
cbm_store_t *w = cbm_store_open_path(lp.dbpath);
ASSERT_NOT_NULL(w);
ASSERT_EQ(sqlite3_exec(cbm_store_get_db(w), "UPDATE lsp_surface SET included_files = ''", NULL,
NULL, NULL),
SQLITE_OK);
ASSERT_EQ(cbm_store_set_format_version(w, 6), CBM_STORE_OK);
cbm_store_close(w);
/* Only the header changes. */
char path[700];
snprintf(path, sizeof(path), "%s/lib/m.h", lp.tmpdir);
FILE *fp = fopen(path, "wb");
ASSERT_NOT_NULL(fp);
fputs("#ifndef M_H\n#define M_H\n#define DO_WORK(x) other_target(x)\n#endif\n", fp);
fclose(fp);
char *resp = index_capture(&lp);
ASSERT_NOT_NULL(resp);
free(resp);
cbm_store_t *r = cbm_store_open_path(lp.dbpath);
ASSERT_NOT_NULL(r);
ASSERT_EQ(include_caller_calls(r, "other_target"), 1);
ASSERT_EQ(include_caller_calls(r, "real_target"), 0);
if (!strstr(g_log_buf, "format_change_reindex")) {
FAIL("a format-6 index was not routed through the full-reindex path");
}
ASSERT_EQ(include_rows_recorded(r), 1);
int format = -1;
ASSERT_EQ(cbm_store_get_format_version(r, &format), CBM_STORE_OK);
ASSERT_EQ(format, CBM_INDEX_FORMAT_VERSION);
rh_cleanup(&lp, r);
PASS();
}
SUITE(index_format) {
RUN_TEST(index_format_siblings_distinct_and_searchable);
RUN_TEST(index_format_legacy_index_rebuilds_and_repairs);
@@ -575,4 +658,5 @@ SUITE(index_format) {
RUN_TEST(index_format_swift_collisions_and_stale_calls_rebuild);
RUN_TEST(index_format_v2_images_rebuild_without_invented_host);
RUN_TEST(index_format_v5_scala_packages_rebuild);
RUN_TEST(index_format_v6_included_files_rebuild);
}
+17 -132
View File
@@ -11,7 +11,7 @@ int tf_deselected_count = 0;
#include "test_framework.h"
#include "test_helpers.h"
#include "test_daemon_runtime_contract.h"
#include "test_image_probe.h"
#include "foundation/compat.h" /* cbm_setenv — #845 supervisor kill switch */
#include "foundation/compat_fs.h" /* cbm_fopen — worker response file */
#include "foundation/constants.h" /* CBM_SZ_4K — forced stderr buffer */
@@ -48,10 +48,6 @@ int tf_deselected_count = 0;
#include <limits.h> /* INT_MAX: a handed-down descriptor */
#include <sys/wait.h> /* waitpid: the process-tree probe root */
#include <unistd.h>
#ifdef __APPLE__
#include <fcntl.h>
#include <sys/mman.h>
#endif
#ifdef CBM_TEST_COVERAGE
#include <dlfcn.h> /* the libc functions behind the profile-keeping ones */
#include <fcntl.h> /* the marker of a forked child */
@@ -657,129 +653,12 @@ static int tf_maybe_run_version_cohort_crash_holder(int argc, char **argv) {
#endif
}
static int tf_maybe_run_runtime_image_holder(int argc, char **argv) {
#ifdef _WIN32
if (argc != 3 || strcmp(argv[1], "__cbm_runtime_image_holder") != 0) {
return -1;
}
HANDLE ready = OpenEventA(EVENT_MODIFY_STATE, FALSE, argv[2]);
bool announced = ready && SetEvent(ready) != 0;
if (ready) {
(void)CloseHandle(ready);
}
if (!announced) {
return 24;
}
Sleep(INFINITE);
return 25;
#else
/* POSIX copied-image holder: block reading stdin until the parent closes
* the release pipe, exactly like the cat(1) donor this replaced. A system
* utility cannot serve as the copied image — a multi-call coreutils
* binary (uutils cat) refuses to execute under the copied name. */
if (argc != 2 || strcmp(argv[1], "__cbm_runtime_image_holder") != 0) {
return -1;
}
char release[16];
ssize_t count;
do {
count = read(STDIN_FILENO, release, sizeof(release));
} while (count > 0 || (count < 0 && errno == EINTR));
return count == 0 ? 0 : 25;
#endif
}
/* Real copied-image HELLO probe used by daemon_runtime. Keeping this in the
* test runner avoids any production-only test hook: the daemon authenticates
* and fingerprints an ordinary, separately executed process image. */
static int tf_maybe_run_runtime_hello_client(int argc, char **argv) {
if (argc != 6 || strcmp(argv[1], "__cbm_runtime_hello_client") != 0) {
return -1;
}
cbm_daemon_ipc_endpoint_t *endpoint = cbm_daemon_ipc_endpoint_new(argv[3], argv[2]);
cbm_daemon_build_identity_t identity = {
.semantic_version = argv[4],
.build_fingerprint = argv[5],
};
cbm_daemon_runtime_connect_result_t result;
memset(&result, 0, sizeof(result));
cbm_daemon_runtime_client_t *client =
endpoint ? cbm_daemon_runtime_client_connect(endpoint, &identity,
TF_RUNTIME_IMAGE_EXCHANGE_TIMEOUT_MS, &result)
: NULL;
bool accepted = client && result.status == CBM_DAEMON_RUNTIME_CONNECT_ACCEPTED &&
result.hello_status == CBM_DAEMON_HELLO_COMPATIBLE;
bool closed =
!client || cbm_daemon_runtime_client_close(client, TF_RUNTIME_IMAGE_EXCHANGE_TIMEOUT_MS);
cbm_daemon_ipc_endpoint_free(endpoint);
if (!accepted) {
return 26;
}
return closed ? 0 : 27;
}
/* Real copied-image activation probe. The request identity is supplied by the
* executing copy so the daemon must authenticate that peer image rather than
* require the active generation's build fingerprint. */
static int tf_maybe_run_runtime_activation_client(int argc, char **argv) {
if (argc != 7 || strcmp(argv[1], "__cbm_runtime_activation_client") != 0) {
return -1;
}
char *action_end = NULL;
unsigned long action_value = strtoul(argv[6], &action_end, 10);
bool action_valid = action_end != argv[6] && *action_end == '\0' &&
action_value >= (unsigned long)CBM_DAEMON_RUNTIME_ACTIVATION_INSTALL &&
action_value <= (unsigned long)CBM_DAEMON_RUNTIME_ACTIVATION_UNINSTALL;
cbm_daemon_ipc_endpoint_t *endpoint =
action_valid ? cbm_daemon_ipc_endpoint_new(argv[3], argv[2]) : NULL;
cbm_daemon_build_identity_t identity = {
.semantic_version = argv[4],
.build_fingerprint = argv[5],
};
cbm_daemon_runtime_activation_result_t result;
memset(&result, 0, sizeof(result));
bool exchanged =
endpoint && cbm_daemon_runtime_request_activation_shutdown(
endpoint, &identity, (cbm_daemon_runtime_activation_action_t)action_value,
TF_RUNTIME_IMAGE_EXCHANGE_TIMEOUT_MS, &result);
cbm_daemon_ipc_endpoint_free(endpoint);
return exchanged && result.accepted ? 0 : 29;
}
/* macOS adversarial HELLO probe: execute this mode from a foreign process
* image while mapping the genuine runner RX. The daemon must authenticate the
* main image, not merely find its own vnode in an arbitrary executable map. */
static int tf_maybe_run_runtime_mapped_hello_client(int argc, char **argv) {
#ifdef __APPLE__
if (argc != 7 || strcmp(argv[1], "__cbm_runtime_mapped_hello_client") != 0) {
return -1;
}
int image_fd = open(argv[2], O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
void *mapping =
image_fd >= 0 ? mmap(NULL, 1, PROT_READ | PROT_EXEC, MAP_PRIVATE, image_fd, 0) : MAP_FAILED;
bool mapped = mapping != MAP_FAILED;
if (image_fd >= 0 && close(image_fd) != 0) {
mapped = false;
}
if (!mapped) {
if (mapping != MAP_FAILED) {
(void)munmap(mapping, 1);
}
return 28;
}
char *hello_argv[] = {argv[0], "__cbm_runtime_hello_client", argv[3], argv[4], argv[5],
argv[6]};
int result = tf_maybe_run_runtime_hello_client(6, hello_argv);
if (munmap(mapping, 1) != 0) {
return 28;
}
return result >= 0 ? result : 28;
#else
(void)argc;
(void)argv;
return -1;
#endif
/* The image probe's roles (tests/image_probe_roles.c, tests/test_image_probe.h).
* The runner itself dispatches only the image holder through here; tf_roles
* names the probe's other literals under TF_PHASE_PROBE so the test-impact
* engine still maps a change those roles reach to the tests that start them. */
static int tf_role_image_probe(int argc, char **argv) {
return tf_image_probe_dispatch(argc, argv);
}
static int tf_maybe_run_mcp_idxfailclosed_probe(int argc, char **argv) {
@@ -1797,6 +1676,8 @@ extern void suite_index_format(void);
extern void suite_fqn(void);
extern void suite_route_canon(void);
extern void suite_path_alias(void);
extern void suite_site_bind(void);
extern void suite_scope_facts(void);
extern void suite_svn_state(void);
extern void suite_watcher(void);
extern void suite_lz4(void);
@@ -2290,6 +2171,7 @@ typedef enum {
TF_PHASE_SEAMED, /* after the spill and build-fingerprint pins */
TF_PHASE_WORKER, /* after the fingerprint capture; the literal anywhere in argv */
TF_PHASE_SPAWNER, /* never dispatched: one more literal a spawner names */
TF_PHASE_PROBE, /* never dispatched here: the image probe runs the role */
} tf_phase_t;
typedef struct {
@@ -2315,10 +2197,11 @@ static const tf_role_t tf_roles[] = {
TF_ROLE(TF_PHASE_INSTALL, "--build-config", tf_role_build_config),
TF_ROLE(TF_PHASE_SEAMED, "--userns-secure-probe", tf_role_userns_secure_probe),
TF_ROLE(TF_PHASE_SEAMED, "__cbm_mcp_idxfailclosed_probe", tf_maybe_run_mcp_idxfailclosed_probe),
TF_ROLE(TF_PHASE_SEAMED, "__cbm_runtime_image_holder", tf_maybe_run_runtime_image_holder),
TF_ROLE(TF_PHASE_SEAMED, "__cbm_runtime_hello_client", tf_maybe_run_runtime_hello_client),
TF_ROLE(TF_PHASE_SEAMED, "__cbm_runtime_activation_client", tf_maybe_run_runtime_activation_client),
TF_ROLE(TF_PHASE_SEAMED, "__cbm_runtime_mapped_hello_client", tf_maybe_run_runtime_mapped_hello_client),
TF_ROLE(TF_PHASE_SEAMED, "__cbm_runtime_image_holder", tf_role_image_probe),
TF_ROLE(TF_PHASE_PROBE, "__cbm_runtime_serve", tf_role_image_probe),
TF_ROLE(TF_PHASE_PROBE, "__cbm_runtime_hello_client", tf_role_image_probe),
TF_ROLE(TF_PHASE_PROBE, "__cbm_runtime_activation_client", tf_role_image_probe),
TF_ROLE(TF_PHASE_PROBE, "__cbm_runtime_mapped_hello_client", tf_role_image_probe),
TF_ROLE(TF_PHASE_SEAMED, "__cbm_version_cohort_crash_holder", tf_maybe_run_version_cohort_crash_holder),
TF_ROLE(TF_PHASE_SEAMED, "__cbm_daemon_ipc_lock_probe", tf_maybe_run_daemon_ipc_lock_probe),
TF_ROLE(TF_PHASE_SEAMED, "__cbm_deleted_self_probe", tf_maybe_run_deleted_self_probe),
@@ -2660,6 +2543,8 @@ int main(int argc, char **argv) {
RUN_SELECTED_SUITE(fqn);
RUN_SELECTED_SUITE(route_canon);
RUN_SELECTED_SUITE(path_alias);
RUN_SELECTED_SUITE(site_bind);
RUN_SELECTED_SUITE(scope_facts);
/* Watcher (M10) */
RUN_SELECTED_SUITE(svn_state);
+61 -1
View File
@@ -5104,6 +5104,48 @@ TEST(tool_search_graph_bm25_ranks_exact_name_first) {
PASS();
}
TEST(tool_search_graph_empty_semantic_query_is_ignored) {
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
ASSERT_NOT_NULL(srv);
cbm_store_t *store = cbm_mcp_server_store(srv);
ASSERT_NOT_NULL(store);
cbm_mcp_server_set_project(srv, "bm25-find");
bm25_findability_fixture(store, "bm25-find");
const char *cases[] = {
"\"query\":\"Table\",\"label\":\"Class\",",
"\"query\":\"Table\",\"semantic_limit\":0,",
"\"query\":\"\",",
"\"name_pattern\":\"^Table$\",",
"",
};
const char *formats[] = {"json", "tree"};
for (size_t f = 0; f < sizeof(formats) / sizeof(formats[0]); f++) {
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
char args[512];
snprintf(args, sizeof(args), "{\"project\":\"bm25-find\",%s\"format\":\"%s\"}",
cases[i], formats[f]);
char *baseline = cbm_mcp_handle_tool(srv, "search_graph", args);
ASSERT_NOT_NULL(baseline);
ASSERT_NULL(strstr(baseline, "\"isError\":true"));
ASSERT_NOT_NULL(strstr(baseline, "Table"));
snprintf(args, sizeof(args),
"{\"project\":\"bm25-find\",%s\"semantic_query\":[],\"format\":\"%s\"}",
cases[i], formats[f]);
char *response = cbm_mcp_handle_tool(srv, "search_graph", args);
ASSERT_NOT_NULL(response);
ASSERT_NULL(strstr(response, "\"isError\":true"));
ASSERT_STR_EQ(response, baseline);
free(response);
free(baseline);
}
}
cbm_mcp_server_free(srv);
PASS();
}
/* `query` selects the BM25 fast path, which returns before semantic_query is
* evaluated. Accepting both therefore used to report success while silently
* discarding one of the caller's two requested result sets. The contract is
@@ -5139,12 +5181,29 @@ TEST(tool_search_graph_rejects_bm25_and_semantic_query_together) {
ASSERT_NOT_NULL(response);
ASSERT_NOT_NULL(strstr(response, "\"isError\":true"));
ASSERT_NOT_NULL(strstr(response, "query and semantic_query are mutually exclusive"));
ASSERT_NOT_NULL(strstr(response, "remove the semantic_query key"));
ASSERT_NOT_NULL(strstr(response, "remove the query key entirely"));
ASSERT_NOT_NULL(strstr(response, "do not retry unchanged arguments"));
ASSERT_NULL(strstr(response, "\"search_mode\":\"bm25\""));
free(response);
const char *invalid[] = {"null", "\"meaning\"", "[42]", "[\"meaning\",42]"};
for (size_t i = 0; i < sizeof(invalid) / sizeof(invalid[0]); i++) {
char args[256];
snprintf(args, sizeof(args),
"{\"project\":\"mixed-search-modes\",\"query\":\"mixedneedle\","
"\"semantic_query\":%s,\"format\":\"json\"}",
invalid[i]);
response = cbm_mcp_handle_tool(srv, "search_graph", args);
ASSERT_NOT_NULL(response);
ASSERT_NOT_NULL(strstr(response, "\"isError\":true"));
ASSERT_NULL(strstr(response, "\"search_mode\":\"bm25\""));
free(response);
}
char *tools = cbm_mcp_tools_list();
ASSERT_NOT_NULL(tools);
ASSERT_NOT_NULL(strstr(tools, "Not with query"));
ASSERT_NOT_NULL(strstr(tools, "[] ignored; otherwise omit query."));
free(tools);
cbm_mcp_server_free(srv);
@@ -28818,6 +28877,7 @@ SUITE(mcp) {
RUN_TEST(tool_search_graph_bm25_post_window_filters_rank_every_candidate);
RUN_TEST(tool_search_graph_bm25_empty_window_ranks_every_candidate);
RUN_TEST(tool_search_graph_bm25_ranks_exact_name_first);
RUN_TEST(tool_search_graph_empty_semantic_query_is_ignored);
RUN_TEST(tool_search_graph_rejects_bm25_and_semantic_query_together);
RUN_TEST(tool_search_graph_semantic_ceiling_never_emits_unusable_continuation);
RUN_TEST(tool_search_graph_semantic_pagination_is_lossless_and_independent);
+10 -6
View File
@@ -380,15 +380,19 @@ static cbm_gbuf_t *run_parallel_with_extract_opts_and_mutator(
CBMModuleDefIndex *module_def_index =
all_defs ? cbm_pxc_build_module_def_index(all_defs, def_count) : NULL;
/* C# has no per-file cross path (cbm_pxc_run_one has no C# case): production
* resolves it only through the Tier-2 registry, so a harness run that opts
* in builds that one registry exactly as run_parallel_pipeline does. */
CBMCrossLspRegistries harness_registries = {0};
/* C# has no per-file cross path (its engine row has no per-file walk):
* production resolves it only through the Tier-2 registry, so a harness run
* that opts in builds that one registry exactly as run_parallel_pipeline
* does. */
CBMCrossLspRegistries harness_registries;
memset(&harness_registries, 0, sizeof(harness_registries));
if (g_harness_cs_registry && all_defs) {
harness_registries.cs = cbm_cs_build_cross_registry(&cross_arena, all_defs, def_count);
(void)cbm_pxc_build_engine_registry(cbm_pxc_engine_of_lang(CBM_LANG_CSHARP), &cross_arena,
all_defs, def_count, &harness_registries);
}
if (g_harness_c_registry && all_defs) {
harness_registries.c = cbm_c_build_cross_registry(&cross_arena, all_defs, def_count);
(void)cbm_pxc_build_engine_registry(cbm_pxc_engine_of_lang(CBM_LANG_C), &cross_arena,
all_defs, def_count, &harness_registries);
}
cbm_parallel_resolve(&ctx, files, file_count, result_cache, &shared_ids, worker_count, all_defs,
def_count, def_modules, module_def_index,
+604
View File
@@ -4884,6 +4884,216 @@ TEST(incremental_shell_deploy_edit_keeps_its_edges) {
PASS();
}
/* Discovery indexes the configuration files of a deploy/ directory and skips
* its program source; every incremental route rediscovers with the same rule,
* so a manifest added there later reaches the graph, and the result equals a
* fresh full index of the same tree. Two additions: the first takes the route
* the planner picks, the second the forced full-load (legacy partial) route;
* then an edit of a manifest there (closure repair declines a changed
* Kubernetes manifest: full route), an edit of the program source there,
* which changes no input (no-op route), and an edit of the deploy script
* (closure repair). */
static int deploy_inc_files(const char *db_path) {
return k8s_inc_count(db_path, "SELECT COUNT(*) FROM nodes WHERE label='File' AND "
"file_path LIKE 'deploy/%'");
}
/* A fresh full index of `repo` into its own database beside it (in `tmp`). */
static int deploy_inc_fresh_files(const char *tmp, const char *repo, const char *name) {
char fresh[512];
snprintf(fresh, sizeof(fresh), "%s/%s", tmp, name);
return k8s_inc_index(repo, fresh) == 0 ? deploy_inc_files(fresh) : -1;
}
/* An Ansible playbook naming a role, the common content of a deploy/
* directory, indexed through the sequential semantic pass: a small tree at
* the default worker count (fewer than 51 files never take the parallel
* pass), the same tree at one worker, and a tree of more than 50 files at one
* worker. Each must index and draw the role import main draws for the same
* files under any other directory name. `workers` NULL keeps the default.
* This runner's memory core is libc malloc/free, so a fault on this path that
* needs the production allocator binding shows in the smoke instead
* (scripts/smoke-test.sh, Phase 2), which indexes the same playbook with the
* shipped binary. */
static int ansible_deploy_index(const char *tmp, const char *name, int padding, const char *workers,
int *imports, int *nodes) {
char repo[400];
char db_path[512];
snprintf(repo, sizeof(repo), "%s/%s", tmp, name);
snprintf(db_path, sizeof(db_path), "%s/%s.db", tmp, name);
write_temp_file(repo, "app/main.py", "def serve():\n return 1\n");
write_temp_file(repo, "deploy/site.yml", "- hosts: web\n roles:\n - web\n");
write_temp_file(repo, "deploy/roles/web/tasks/main.yml",
"- name: Install\n apt:\n name: nginx\n");
for (int i = 0; i < padding; i++) {
char rel[64];
char body[64];
snprintf(rel, sizeof(rel), "app/m%d.py", i);
snprintf(body, sizeof(body), "def f%d():\n return %d\n", i, i);
write_temp_file(repo, rel, body);
}
char saved[32] = "";
const char *was = getenv("CBM_WORKERS");
bool had = was != NULL;
if (had) {
snprintf(saved, sizeof(saved), "%s", was);
}
if (workers) {
cbm_setenv("CBM_WORKERS", workers, 1);
}
int rc = k8s_inc_index(repo, db_path);
if (workers && had) {
cbm_setenv("CBM_WORKERS", saved, 1);
} else if (workers) {
cbm_unsetenv("CBM_WORKERS");
}
*imports = rc == 0
? k8s_inc_count(db_path, "SELECT COUNT(*) FROM edges e JOIN nodes s ON "
"s.id = e.source_id JOIN nodes t ON t.id = e.target_id "
"WHERE e.type = 'IMPORTS' AND s.file_path = "
"'deploy/site.yml' AND t.label = 'Folder' AND "
"t.file_path = 'deploy/roles/web' AND "
"json_extract(e.properties, '$.via') = 'ansible'")
: -1;
*nodes = rc == 0 ? k8s_inc_count(db_path, "SELECT COUNT(*) FROM nodes") : -1;
return rc;
}
TEST(pipeline_ansible_under_deploy_sequential_pass) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "/tmp/cbm_ansdep_XXXXXX");
if (!cbm_mkdtemp(tmp)) {
FAIL("tmpdir");
}
int imports_default = -1, nodes_default = -1;
int imports_one = -1, nodes_one = -1;
int imports_big = -1, nodes_big = -1;
int rc_default = ansible_deploy_index(tmp, "small", 0, NULL, &imports_default, &nodes_default);
int rc_one = ansible_deploy_index(tmp, "small1", 0, "1", &imports_one, &nodes_one);
int rc_big = ansible_deploy_index(tmp, "big1", 60, "1", &imports_big, &nodes_big);
th_rmtree(tmp);
ASSERT_EQ(rc_default, 0);
ASSERT_EQ(imports_default, 1);
ASSERT_EQ(rc_one, 0);
ASSERT_EQ(imports_one, 1);
ASSERT_EQ(nodes_one, nodes_default); /* the worker count changes nothing */
ASSERT_EQ(rc_big, 0);
ASSERT_EQ(imports_big, 1);
ASSERT_GT(nodes_big, nodes_one); /* the padding is indexed too */
PASS();
}
TEST(incremental_sees_config_added_under_deploy) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "/tmp/cbm_depinc_XXXXXX");
if (!cbm_mkdtemp(tmp)) {
FAIL("tmpdir");
}
char repo[300];
snprintf(repo, sizeof(repo), "%s/repo", tmp);
write_temp_file(repo, "app.py", "def serve():\n return 1\n");
write_temp_file(repo, "deploy/main.py", "def serve():\n return 2\n");
write_temp_file(repo, "deploy/k8s/app.yaml",
"apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: app\n");
write_temp_file(repo, "deploy/install.sh",
"#!/bin/sh\ncd \"$(dirname \"$0\")\"\nkubectl apply -f k8s/app.yaml\n");
char db_path[512];
snprintf(db_path, sizeof(db_path), "%s/depinc.db", tmp);
static const char deploys[] = "SELECT COUNT(*) FROM edges WHERE type='DEPLOYS'";
static const char copied[] = "SELECT COUNT(*) FROM nodes WHERE file_path='deploy/main.py'";
/* the same function outside deploy/ is indexed: the rule is what drops the copy */
static const char original[] = "SELECT COUNT(*) FROM nodes WHERE file_path='app.py' AND "
"label='Function' AND name='serve'";
static const char added[] = "SELECT COUNT(*) FROM nodes WHERE label='File' AND "
"file_path IN ('deploy/k8s/extra.yaml','deploy/k8s/more.yaml')";
int rc_full = k8s_inc_index(repo, db_path);
int files_full = deploy_inc_files(db_path);
int copied_full = k8s_inc_count(db_path, copied);
int original_full = k8s_inc_count(db_path, original);
write_temp_file(repo, "deploy/k8s/extra.yaml",
"apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: extra\n");
int rc_first = k8s_inc_index(repo, db_path);
cbm_incremental_route_t first_route = cbm_pipeline_incremental_test_last_route();
int files_first = deploy_inc_files(db_path);
int fresh_first = deploy_inc_fresh_files(tmp, repo, "fresh1.db");
write_temp_file(repo, "deploy/k8s/more.yaml",
"apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: more\n");
#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
cbm_pipeline_incremental_test_force_legacy_partial_once();
#endif
int rc_second = k8s_inc_index(repo, db_path);
cbm_incremental_route_t second_route = cbm_pipeline_incremental_test_last_route();
int files_second = deploy_inc_files(db_path);
int fresh_second = deploy_inc_fresh_files(tmp, repo, "fresh2.db");
int added_second = k8s_inc_count(db_path, added);
int copied_second = k8s_inc_count(db_path, copied);
int original_second = k8s_inc_count(db_path, original);
/* an edit of a manifest there: the route an edit takes (closure repair
* unless it declines), against a fresh full index again */
static const char renamed[] = "SELECT COUNT(*) FROM nodes WHERE label='Resource' AND "
"file_path='deploy/k8s/extra.yaml' AND "
"qualified_name LIKE '%extra2%'";
write_temp_file(repo, "deploy/k8s/extra.yaml",
"apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: extra2\n");
int rc_third = k8s_inc_index(repo, db_path);
cbm_incremental_route_t third_route = cbm_pipeline_incremental_test_last_route();
int renamed_third = k8s_inc_count(db_path, renamed);
char fresh3[512];
snprintf(fresh3, sizeof(fresh3), "%s/fresh3.db", tmp);
int renamed_fresh = k8s_inc_index(repo, fresh3) == 0 ? k8s_inc_count(fresh3, renamed) : -1;
/* an edit of the program source there changes no input: nothing to do */
write_temp_file(repo, "deploy/main.py", "def serve():\n return 3\n");
int rc_fourth = k8s_inc_index(repo, db_path);
cbm_incremental_route_t fourth_route = cbm_pipeline_incremental_test_last_route();
int copied_fourth = k8s_inc_count(db_path, copied);
/* an edit of the script there: closure repair, as for any script */
write_temp_file(
repo, "deploy/install.sh",
"#!/bin/sh\ncd \"$(dirname \"$0\")\"\nkubectl apply -f k8s/app.yaml\n# edited\n");
int rc_fifth = k8s_inc_index(repo, db_path);
cbm_incremental_route_t fifth_route = cbm_pipeline_incremental_test_last_route();
int deploys_fifth = k8s_inc_count(db_path, deploys);
char fresh5[512];
snprintf(fresh5, sizeof(fresh5), "%s/fresh5.db", tmp);
int deploys_fresh = k8s_inc_index(repo, fresh5) == 0 ? k8s_inc_count(fresh5, deploys) : -1;
th_rmtree(tmp);
ASSERT_EQ(rc_full, 0);
ASSERT_EQ(files_full, 2); /* app.yaml and install.sh; deploy/main.py is program source */
ASSERT_EQ(copied_full, 0);
ASSERT_EQ(original_full, 1);
ASSERT_EQ(rc_first, 0);
ASSERT_NEQ(first_route, CBM_INCREMENTAL_ROUTE_NONE);
ASSERT_NEQ(first_route, CBM_INCREMENTAL_ROUTE_NOOP);
ASSERT_EQ(files_first, 3);
ASSERT_EQ(fresh_first, files_first);
ASSERT_EQ(rc_second, 0);
ASSERT_EQ(second_route, CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL);
ASSERT_EQ(files_second, 4);
ASSERT_EQ(fresh_second, files_second);
ASSERT_EQ(added_second, 2);
ASSERT_EQ(copied_second, 0);
ASSERT_EQ(original_second, 1);
ASSERT_EQ(rc_third, 0);
ASSERT_NEQ(third_route, CBM_INCREMENTAL_ROUTE_NONE);
ASSERT_NEQ(third_route, CBM_INCREMENTAL_ROUTE_NOOP);
ASSERT_EQ(renamed_third, 1);
ASSERT_EQ(renamed_fresh, renamed_third);
ASSERT_EQ(rc_fourth, 0);
ASSERT_EQ(fourth_route, CBM_INCREMENTAL_ROUTE_NOOP);
ASSERT_EQ(copied_fourth, 0);
ASSERT_EQ(rc_fifth, 0);
ASSERT_EQ(fifth_route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);
ASSERT_EQ(deploys_fifth, 1);
ASSERT_EQ(deploys_fresh, deploys_fifth);
PASS();
}
/* Deleting the only file that starts a program leaves no Process node
* behind -- a full index never makes one without a SPAWNS edge. One script
* (pass_shell.c) and one Python call (pass_spawns.c) start programs; the
@@ -35512,7 +35722,399 @@ TEST(pipeline_inc_c_family_fragments) {
th_rmtree(tmp);
PASS();
}
/* ── C/C++ #include lookup against the indexed repository root (#2993) ── */
#ifdef _WIN32
#include <direct.h>
#define inc_root_chdir _chdir
#define inc_root_getcwd _getcwd
#else
#define inc_root_chdir chdir
#define inc_root_getcwd getcwd
#endif
/* Index `root` into `db` with the process working directory set to `cwd`, and
* restore the working directory afterwards. */
static int inc_root_index_from(const char *root, const char *db, const char *cwd) {
char saved[CBM_SZ_4K];
if (!inc_root_getcwd(saved, sizeof(saved)) || inc_root_chdir(cwd) != 0) {
return -1;
}
int rc = -1;
cbm_pipeline_t *p = cbm_pipeline_new(root, db, CBM_MODE_FULL);
if (p) {
(void)cbm_pipeline_set_project_name(p, "incroot");
cbm_pipeline_set_persistence(p, false);
rc = cbm_pipeline_run(p);
cbm_pipeline_free(p);
}
if (inc_root_chdir(saved) != 0) {
rc = -1;
}
return rc;
}
/* One FNV-1a digest over every node and edge row of the graph, in sorted
* order: two graphs with the same digest and row count are the same graph. */
static uint64_t inc_root_graph_digest(const char *db_path, int *rows) {
static const char *const queries[] = {
"SELECT label||'|'||qualified_name||'|'||file_path||'|'||start_line||'|'||end_line||'|'||"
"properties FROM nodes ORDER BY 1",
"SELECT e.type||'|'||s.qualified_name||'|'||t.qualified_name||'|'||e.properties FROM edges "
"e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON t.id=e.target_id ORDER BY 1",
};
uint64_t h = 1469598103934665603ULL;
*rows = 0;
sqlite3 *db = NULL;
if (sqlite3_open_v2(db_path, &db, SQLITE_OPEN_READONLY, NULL) == SQLITE_OK) {
for (size_t q = 0; q < sizeof(queries) / sizeof(queries[0]); q++) {
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(db, queries[q], -1, &stmt, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char *row = sqlite3_column_text(stmt, 0);
for (const unsigned char *c = row ? row : (const unsigned char *)""; *c; c++) {
h = (h ^ *c) * 1099511628211ULL;
}
h = (h ^ 0xffU) * 1099511628211ULL;
(*rows)++;
}
}
sqlite3_finalize(stmt);
}
}
sqlite3_close(db);
return h;
}
/* CALLS edges from the function named `from` to the Function named `to`. */
static int inc_root_calls(const char *db, const char *from, const char *to) {
char sql[512];
snprintf(sql, sizeof(sql),
"SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON "
"t.id=e.target_id WHERE e.type='CALLS' AND s.name='%s' AND t.name='%s' AND "
"t.label='Function'",
from, to);
return k8s_inc_count(db, sql);
}
/* #2993: the graph depended on the directory the indexer was started from:
* a quoted #include was looked up relative to the process working directory.
* The lookup now resolves against the indexed repository root. The same
* fixture, indexed from an empty directory, from a directory holding a
* different lib/m.h, and from the repository root, gives one graph, in which
* the macro of the repository's own lib/m.h expands to a call that resolves
* to its in-tree target. */
TEST(pipeline_c_include_lookup_uses_repo_root) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "%s/cbm_incroot_XXXXXX", cbm_tmpdir());
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
char root[512];
char empty[512];
char other[512];
snprintf(root, sizeof(root), "%s/repo", tmp);
snprintf(empty, sizeof(empty), "%s/empty", tmp);
snprintf(other, sizeof(other), "%s/other", tmp);
write_temp_file(root, "lib/a.c",
"#define A_C_LOCAL 1\n"
"#include \"m.h\"\n"
"\n"
"void caller(int v) {\n"
" DO_WORK(v);\n"
"}\n");
write_temp_file(root, "lib/m.h",
"#ifndef M_H\n#define M_H\n#define DO_WORK(x) real_target(x)\n#endif\n");
write_temp_file(root, "lib/t.c",
"void real_target(int x) { (void)x; }\n"
"void other_target(int x) { (void)x; }\n");
write_temp_file(other, "lib/m.h",
"#ifndef M_H\n#define M_H\n#define DO_WORK(x) other_target(x)\n#endif\n");
ASSERT(cbm_mkdir_p(empty, 0755));
const char *cwds[] = {empty, other, root};
enum { RUNS = sizeof(cwds) / sizeof(cwds[0]) };
int rc[RUNS];
int rows[RUNS];
int real[RUNS];
int stray[RUNS];
uint64_t digest[RUNS];
for (int i = 0; i < RUNS; i++) {
char db[512];
snprintf(db, sizeof(db), "%s/graph%d.db", tmp, i);
rc[i] = inc_root_index_from(root, db, cwds[i]);
digest[i] = inc_root_graph_digest(db, &rows[i]);
real[i] = inc_root_calls(db, "caller", "real_target");
stray[i] = inc_root_calls(db, "caller", "other_target");
}
th_rmtree(tmp);
for (int i = 0; i < RUNS; i++) {
ASSERT_EQ(rc[i], 0);
ASSERT_GT(rows[i], 0);
ASSERT_EQ(real[i], 1);
ASSERT_EQ(stray[i], 0);
ASSERT_EQ(rows[i], rows[0]);
ASSERT(digest[i] == digest[0]);
}
PASS();
}
/* #2993: simplecpp discards a file's whole expansion when it stops at #error.
* An #error reached inside an included header must not cost the file its
* expansion: the file keeps its expansion without includes, so the call its
* own macro expands to survives. Indexed from the repository root, where the
* header is found whichever way the lookup is made. */
TEST(pipeline_c_include_error_keeps_expansion_without_includes) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "%s/cbm_incerr_XXXXXX", cbm_tmpdir());
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
char root[512];
snprintf(root, sizeof(root), "%s/repo", tmp);
write_temp_file(root, "cfg.h",
"#ifndef CFG_H\n"
"#define CFG_H\n"
"#ifndef CFG_CONFIGURED\n"
"#error \"configure first\"\n"
"#endif\n"
"#endif\n");
write_temp_file(root, "z.c",
"#include \"cfg.h\"\n"
"#define RUN_STEP(x) step_target(x)\n"
"\n"
"int z_entry(int v) {\n"
" RUN_STEP(v);\n"
" return v;\n"
"}\n");
write_temp_file(root, "steps.c", "void step_target(int x) { (void)x; }\n");
char db[512];
snprintf(db, sizeof(db), "%s/graph.db", tmp);
int rc = inc_root_index_from(root, db, root);
int step_calls = inc_root_calls(db, "z_entry", "step_target");
th_rmtree(tmp);
ASSERT_EQ(rc, 0);
ASSERT_EQ(step_calls, 1);
PASS();
}
/* #2993: an included header's body is the header's own -- indexed as its own
* file, with its own lines. The includer's expansion holds only the
* includer's tokens (the header's macros still shape them), so a call or a
* write inside a header's inline function is never one of the includer's
* edges, and no edge of the includer carries a line past the includer's last
* line. Indexed from the repository root, where the header is found whichever
* way the lookup is made. */
TEST(pipeline_c_header_body_stays_with_the_header) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "%s/cbm_inchdr_XXXXXX", cbm_tmpdir());
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
char root[512];
snprintf(root, sizeof(root), "%s/repo", tmp);
char header[2048];
int used = snprintf(header, sizeof(header), "#ifndef OPS_H\n#define OPS_H\n");
for (int i = 0; i < 24; i++) {
used += snprintf(header + used, sizeof(header) - (size_t)used, "/* line %d */\n", i);
}
snprintf(header + used, sizeof(header) - (size_t)used,
"extern int header_counter;\n"
"static inline int ops_inline(int v) {\n"
" header_counter = v;\n"
" return header_only_callee(v);\n"
"}\n"
"#endif\n");
write_temp_file(root, "ops.h", header);
/* user.c is 6 lines long. */
write_temp_file(root, "user.c",
"#include \"ops.h\"\n"
"#define USER_FLAG 1\n"
"\n"
"int user_fn(int v) {\n"
" return main_only_callee(v);\n"
"}\n");
write_temp_file(root, "callees.c",
"int header_counter;\n"
"int header_only_callee(int v) { return v; }\n"
"int main_only_callee(int v) { return v; }\n");
char db[512];
snprintf(db, sizeof(db), "%s/graph.db", tmp);
int rc = inc_root_index_from(root, db, root);
int from_user = k8s_inc_count(
db, "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON "
"t.id=e.target_id WHERE e.type IN ('CALLS','USAGE','WRITES') AND "
"s.file_path='user.c' AND t.name IN ('header_only_callee','header_counter')");
int past_end = k8s_inc_count(
db, "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id WHERE e.type IN "
"('CALLS','USAGE') AND s.file_path='user.c' AND "
"CAST(json_extract(e.properties,'$.line') AS INTEGER) > 6");
int own_call = k8s_inc_count(
db, "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON "
"t.id=e.target_id WHERE e.type='CALLS' AND s.file_path='ops.h' AND "
"t.name='header_only_callee'");
int user_call = inc_root_calls(db, "user_fn", "main_only_callee");
th_rmtree(tmp);
ASSERT_EQ(rc, 0);
ASSERT_EQ(from_user, 0);
ASSERT_EQ(past_end, 0);
ASSERT_GT(own_call, 0);
ASSERT_EQ(user_call, 1);
PASS();
}
/* #2993: only a name that denotes a file of the indexed tree resolves. Each
* of the three includes of lib/a.c names a header that is not part of the
* indexed tree (the third is reached through a symbolic link, which discovery
* does not index either), so none of the three macros expands, wherever the
* indexer runs; here it runs from the repository root. */
TEST(pipeline_c_include_lookup_stays_in_repo_root) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "%s/cbm_incside_XXXXXX", cbm_tmpdir());
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
char root[512];
snprintf(root, sizeof(root), "%s/repo", tmp);
write_temp_file(tmp, "m_side.h", "#define SIDE_M(x) m_target(x)\n");
write_temp_file(tmp, "n_side.h", "#define SIDE_N(x) n_target(x)\n");
write_temp_file(tmp, "l_side.h", "#define SIDE_L(x) l_target(x)\n");
char source[2048];
snprintf(source, sizeof(source),
"#define A_LOCAL 1\n"
"#include \"../../m_side.h\"\n"
"#include \"%s/n_side.h\"\n"
"#include \"l_side.h\"\n"
"\n"
"void caller(int v) {\n"
" SIDE_M(v);\n"
" SIDE_N(v);\n"
" SIDE_L(v);\n"
"}\n",
tmp);
write_temp_file(root, "lib/a.c", source);
write_temp_file(root, "lib/t.c",
"void m_target(int x) { (void)x; }\n"
"void n_target(int x) { (void)x; }\n"
"void l_target(int x) { (void)x; }\n");
#ifndef _WIN32
char link_target[512];
char link_path[512];
snprintf(link_target, sizeof(link_target), "%s/l_side.h", tmp);
snprintf(link_path, sizeof(link_path), "%s/lib/l_side.h", root);
ASSERT_EQ(symlink(link_target, link_path), 0);
#endif
char db[512];
snprintf(db, sizeof(db), "%s/graph.db", tmp);
int rc = inc_root_index_from(root, db, root);
static const char *const targets[] = {"m_target", "n_target", "l_target"};
int wrong = 0;
for (size_t i = 0; i < sizeof(targets) / sizeof(targets[0]); i++) {
int n = inc_root_calls(db, "caller", targets[i]);
if (n != 0) {
printf(" caller -> %s: %d\n", targets[i], n);
wrong++;
}
}
th_rmtree(tmp);
ASSERT_EQ(rc, 0);
ASSERT_EQ(wrong, 0);
PASS();
}
/* #2993: a C file's graph depends on the in-tree headers its includes read,
* so an edit to a header alone changes its includers' graphs. lib/a.c
* includes lib/m.h directly, lib/b.c through lib/mid.h; only lib/m.h's macro
* changes. The incremental refresh (closure repair) must re-extract both
* includers and give the graph a fresh full index gives. 60 filler files take
* the fixture past MIN_FILES_FOR_PARALLEL, so the fresh full index resolves
* calls on the same parallel path as the repair: the sequential path writes
* the same CALLS properties in another key order. */
TEST(pipeline_c_header_edit_refreshes_includers_incrementally) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "%s/cbm_incedit_XXXXXX", cbm_tmpdir());
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
char root[512];
snprintf(root, sizeof(root), "%s/repo", tmp);
write_temp_file(root, "lib/m.h",
"#ifndef M_H\n#define M_H\n#define DO_WORK(x) real_target(x)\n#endif\n");
write_temp_file(root, "lib/mid.h", "#ifndef MID_H\n#define MID_H\n#include \"m.h\"\n#endif\n");
write_temp_file(root, "lib/a.c",
"#define A_LOCAL 1\n"
"#include \"m.h\"\n"
"\n"
"void caller_a(int v) {\n"
" DO_WORK(v);\n"
"}\n");
write_temp_file(root, "lib/b.c",
"#define B_LOCAL 1\n"
"#include \"mid.h\"\n"
"\n"
"void caller_b(int v) {\n"
" DO_WORK(v);\n"
"}\n");
write_temp_file(root, "lib/t.c",
"void real_target(int x) { (void)x; }\n"
"void other_target(int x) { (void)x; }\n");
write_temp_file(root, "lib/u.c", "int unrelated(void) { return 1; }\n");
for (int i = 0; i < 60; i++) {
char name[64];
char body[96];
snprintf(name, sizeof(name), "fill/fill_%02d.c", i);
snprintf(body, sizeof(body), "int fill_%02d(void) { return %d; }\n", i, i);
write_temp_file(root, name, body);
}
char db[512];
char fresh_db[512];
snprintf(db, sizeof(db), "%s/graph.db", tmp);
snprintf(fresh_db, sizeof(fresh_db), "%s/fresh.db", tmp);
int rc_full = inc_root_index_from(root, db, root);
int before_a = inc_root_calls(db, "caller_a", "real_target");
write_temp_file(root, "lib/m.h",
"#ifndef M_H\n#define M_H\n#define DO_WORK(x) other_target(x)\n#endif\n");
cbm_pipeline_incremental_test_reset_faults();
int rc_incr = inc_root_index_from(root, db, root);
cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();
int rc_fresh = inc_root_index_from(root, fresh_db, root);
int incr_rows = 0;
int fresh_rows = 0;
uint64_t incr_digest = inc_root_graph_digest(db, &incr_rows);
uint64_t fresh_digest = inc_root_graph_digest(fresh_db, &fresh_rows);
unsigned char incr_content[32] = {0};
unsigned char fresh_content[32] = {1};
cbm_store_t *incr_store = cbm_store_open_path(db);
cbm_store_t *fresh_store = cbm_store_open_path(fresh_db);
bool contents_read =
incr_store && fresh_store &&
cbm_store_graph_content_digest(incr_store, "incroot", incr_content) == CBM_STORE_OK &&
cbm_store_graph_content_digest(fresh_store, "incroot", fresh_content) == CBM_STORE_OK;
cbm_store_close(incr_store);
cbm_store_close(fresh_store);
static const char *const callers[] = {"caller_a", "caller_b"};
int wrong = 0;
for (size_t i = 0; i < sizeof(callers) / sizeof(callers[0]); i++) {
int now = inc_root_calls(db, callers[i], "other_target");
int stale = inc_root_calls(db, callers[i], "real_target");
if (now != 1 || stale != 0) {
printf(" %s: other_target %d, real_target %d\n", callers[i], now, stale);
wrong++;
}
}
th_rmtree(tmp);
ASSERT_EQ(rc_full, 0);
ASSERT_EQ(before_a, 1);
ASSERT_EQ(rc_incr, 0);
ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);
ASSERT_EQ(rc_fresh, 0);
ASSERT_EQ(wrong, 0);
ASSERT_EQ(incr_rows, fresh_rows);
ASSERT(incr_digest == fresh_digest);
ASSERT(contents_read);
ASSERT(memcmp(incr_content, fresh_content, sizeof(incr_content)) == 0);
PASS();
}
SUITE(pipeline) {
RUN_TEST(pipeline_c_header_edit_refreshes_includers_incrementally);
RUN_TEST(pipeline_c_include_lookup_uses_repo_root);
RUN_TEST(pipeline_c_include_error_keeps_expansion_without_includes);
RUN_TEST(pipeline_c_header_body_stays_with_the_header);
RUN_TEST(pipeline_c_include_lookup_stays_in_repo_root);
RUN_TEST(pipeline_inc_c_family_fragments);
RUN_TEST(pipeline_python_module_callers_sequential);
RUN_TEST(pipeline_python_module_callers_parallel);
@@ -36086,6 +36688,8 @@ SUITE(pipeline_semantic_manifest_repro) {
RUN_TEST(pipeline_staged_publish_commits_below_full_sync_issue1419);
RUN_TEST(incremental_k8s_edits_reach_other_files);
RUN_TEST(incremental_shell_deploy_edit_keeps_its_edges);
RUN_TEST(incremental_sees_config_added_under_deploy);
RUN_TEST(pipeline_ansible_under_deploy_sequential_pass);
RUN_TEST(incremental_deleted_spawner_drops_its_process);
RUN_TEST(incremental_deleted_caller_drops_its_route);
RUN_TEST(incremental_script_request_to_compose_service_keeps_host);
+2 -2
View File
@@ -537,12 +537,12 @@ static pc_spill_witness_t pc_collect_spill_witness(pc_fixture_t *f) {
decl = cbm_test_declarations_parse(pc_config0, strlen(pc_config0), false);
if (!decl) goto done;
cache[0] = cbm_extract_file_ex_with_tests(plain, (int)strlen(plain), CBM_LANG_C,
PC_PROJECT, "contributor.c", 0, NULL, NULL, NULL, NULL, decl);
PC_PROJECT, "contributor.c", 0, NULL, NULL, NULL, NULL, decl, NULL);
if (!cache[0] || cache[0]->test_declarations_status != CBM_TEST_EXTRACT_OK || cache[0]->defs.count < 1)
goto done;
cbm_result_compact(cache[0]);
cache[1] = cbm_extract_file_ex_with_tests(owner, (int)strlen(owner), CBM_LANG_C,
PC_PROJECT, "parked.c", 0, NULL, NULL, NULL, NULL, decl);
PC_PROJECT, "parked.c", 0, NULL, NULL, NULL, NULL, decl, NULL);
if (!pc_result_case(cache[1], "CHECK_parked", PC_PROJECT ".parked.CHECK_parked")) goto done;
cbm_result_compact(cache[1]);
ctx.test_declarations = decl;
+331
View File
@@ -0,0 +1,331 @@
/*
* test_scope_facts.c -- the per-file scope facts of the shared cross-file layer
* (cbm.h CBMScopeEvent, internal/cbm/extract_scope.c) and their stored form
* (the "is" key of the LSP surface, src/pipeline/lsp_surface.c).
*
* Protobuf is the one language with a scope row: definitions with their full
* names, includes with `import public` as a re-export, qualified type
* references, parse errors as opaque. Every other language records nothing,
* and its stored surface is unchanged.
*/
#include "test_framework.h"
#include "../src/pipeline/lsp_surface.h"
#include "../src/store/store.h"
#include "../src/foundation/arena.h"
#include "cbm.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char SF_PROTO[] = "syntax = \"proto3\";\n"
"package a.b;\n"
"\n"
"import \"x/common.proto\";\n"
"import public \"x/shared.proto\";\n"
"import /* public */ \"x/plain.proto\";\n"
"\n"
"message Outer {\n"
" message Inner { string s = 1; }\n"
" Inner inner = 1;\n"
" common.Status status = 2;\n"
" .a.b.Outer.Inner abs = 3;\n"
"}\n"
"\n"
"enum Kind { KIND_UNSPECIFIED = 0; }\n";
static CBMFileResult *sf_extract(const char *source, CBMLanguage lang, const char *rel) {
return cbm_extract_file(source, (int)strlen(source), lang, "p", rel, 0, NULL, NULL);
}
/* The event of `kind` named `name` (NULL: the n-th of that kind), or NULL. */
static const CBMScopeEvent *sf_find(const CBMFileResult *r, uint8_t kind, const char *name,
int nth) {
for (int i = 0; i < r->scope.count; i++) {
const CBMScopeEvent *e = &r->scope.items[i];
if (e->kind != kind) {
continue;
}
if (name ? (e->name && strcmp(e->name, name) == 0) : nth-- == 0) {
return e;
}
}
return NULL;
}
static int sf_count(const CBMFileResult *r, uint8_t kind) {
int n = 0;
for (int i = 0; i < r->scope.count; i++) {
n += r->scope.items[i].kind == kind;
}
return n;
}
static const char *sf_module(const CBMFileResult *r, const CBMScopeEvent *e) {
return e && e->index >= 0 && e->index < r->imports.count
? r->imports.items[e->index].module_path
: NULL;
}
TEST(scope_facts_proto_definitions_includes_references) {
CBMFileResult *r = sf_extract(SF_PROTO, CBM_LANG_PROTOBUF, "proto/a.proto");
ASSERT_NOT_NULL(r);
ASSERT_TRUE(cbm_extract_scope_has_row(CBM_LANG_PROTOBUF));
/* includes: one per import statement, the importing package as qual,
* `import public` re-exports, a comment holding the word does not */
ASSERT_EQ(sf_count(r, CBM_SCOPE_INCLUDE), 3);
const CBMScopeEvent *i0 = sf_find(r, CBM_SCOPE_INCLUDE, NULL, 0);
const CBMScopeEvent *i1 = sf_find(r, CBM_SCOPE_INCLUDE, NULL, 1);
const CBMScopeEvent *i2 = sf_find(r, CBM_SCOPE_INCLUDE, NULL, 2);
ASSERT_STR_EQ(sf_module(r, i0), "x/common.proto");
ASSERT_STR_EQ(sf_module(r, i1), "x/shared.proto");
ASSERT_STR_EQ(sf_module(r, i2), "x/plain.proto");
ASSERT_EQ(i0->flags & CBM_SCOPE_REEXPORT, 0);
ASSERT_EQ(i1->flags & CBM_SCOPE_REEXPORT, CBM_SCOPE_REEXPORT);
ASSERT_EQ(i2->flags & CBM_SCOPE_REEXPORT, 0);
ASSERT_STR_EQ(i0->qual, "a.b");
ASSERT_NULL(i0->name);
/* definitions: the extractor's own definition, with its full name */
const CBMScopeEvent *outer = sf_find(r, CBM_SCOPE_DEF, "Outer", 0);
const CBMScopeEvent *inner = sf_find(r, CBM_SCOPE_DEF, "Inner", 0);
const CBMScopeEvent *kind = sf_find(r, CBM_SCOPE_DEF, "Kind", 0);
ASSERT_NOT_NULL(outer);
ASSERT_NOT_NULL(inner);
ASSERT_NOT_NULL(kind);
ASSERT_STR_EQ(outer->qual, "a.b.Outer");
ASSERT_STR_EQ(inner->qual, "a.b.Outer.Inner");
ASSERT_STR_EQ(kind->qual, "a.b.Kind");
ASSERT_TRUE(outer->index >= 0 && outer->index < r->defs.count);
ASSERT_STR_EQ(r->defs.items[outer->index].name, "Outer");
ASSERT_STR_EQ(r->defs.items[inner->index].name, "Inner");
ASSERT_EQ(sf_count(r, CBM_SCOPE_ASSIGN), 0);
/* references as written, qualifier included; index is their start byte */
const CBMScopeEvent *status = sf_find(r, CBM_SCOPE_QREF, "common.Status", 0);
const CBMScopeEvent *abs = sf_find(r, CBM_SCOPE_QREF, ".a.b.Outer.Inner", 0);
ASSERT_NOT_NULL(status);
ASSERT_NOT_NULL(abs);
ASSERT_NOT_NULL(sf_find(r, CBM_SCOPE_QREF, "Inner", 0));
ASSERT_EQ(status->index, (int32_t)(strstr(SF_PROTO, "common.Status") - SF_PROTO));
ASSERT_EQ(sf_count(r, CBM_SCOPE_OPAQUE), 0);
/* source order: an inner definition before its outer one */
for (int i = 1; i < r->scope.count; i++) {
ASSERT_TRUE(r->scope.items[i - 1].pos <= r->scope.items[i].pos);
}
ASSERT_TRUE(inner->pos < outer->pos);
ASSERT_TRUE(outer->pos < kind->pos);
cbm_free_result(r);
PASS();
}
TEST(scope_facts_language_without_row_records_nothing) {
ASSERT_FALSE(cbm_extract_scope_has_row(CBM_LANG_GO));
CBMFileResult *r =
sf_extract("package main\n\nimport \"fmt\"\n\nfunc main() { fmt.Println() }\n", CBM_LANG_GO,
"main.go");
ASSERT_NOT_NULL(r);
ASSERT_GT(r->imports.count, 0);
ASSERT_EQ(r->scope.count, 0);
cbm_free_result(r);
PASS();
}
TEST(scope_facts_parse_error_is_opaque) {
static const char src[] = "syntax = \"proto3\";\n"
"package p;\n"
"message A { string s = 1; !!! }\n"
"message B { string t = 1; }\n";
CBMFileResult *r = sf_extract(src, CBM_LANG_PROTOBUF, "e.proto");
ASSERT_NOT_NULL(r);
const CBMScopeEvent *opaque = sf_find(r, CBM_SCOPE_OPAQUE, NULL, 0);
ASSERT_NOT_NULL(opaque);
ASSERT_EQ(opaque->flags & CBM_SCOPE_CONDITIONAL, CBM_SCOPE_CONDITIONAL);
ASSERT_NOT_NULL(sf_find(r, CBM_SCOPE_DEF, "B", 0));
cbm_free_result(r);
PASS();
}
TEST(scope_facts_survive_compaction) {
CBMFileResult *r = sf_extract(SF_PROTO, CBM_LANG_PROTOBUF, "proto/a.proto");
ASSERT_NOT_NULL(r);
int n = r->scope.count;
ASSERT_GT(n, 0);
char before[4096];
size_t off = 0;
for (int i = 0; i < n; i++) {
const CBMScopeEvent *e = &r->scope.items[i];
off += (size_t)snprintf(before + off, sizeof(before) - off, "%u|%d|%u|%u|%s|%s\n", e->kind,
e->index, e->pos, e->flags, e->name ? e->name : "-",
e->qual ? e->qual : "-");
}
cbm_result_compact(r);
ASSERT_EQ(r->scope.count, n);
char after[4096];
off = 0;
for (int i = 0; i < n; i++) {
const CBMScopeEvent *e = &r->scope.items[i];
off += (size_t)snprintf(after + off, sizeof(after) - off, "%u|%d|%u|%u|%s|%s\n", e->kind,
e->index, e->pos, e->flags, e->name ? e->name : "-",
e->qual ? e->qual : "-");
}
ASSERT_STR_EQ(after, before);
cbm_free_result(r);
PASS();
}
/* One file's stored surface row (cbm_lsp_surface_build_rows, no LSP defs). */
static bool sf_row(CBMFileResult *r, const char *rel, CBMLanguage lang, char **json, char **sha) {
cbm_file_info_t file = {.rel_path = (char *)rel, .language = lang};
cbm_lsp_surface_row_t *rows = NULL;
int count = 0;
int rc = cbm_lsp_surface_build_rows(NULL, "p", &r, &file, 1, NULL, NULL, &rows, &count);
bool ok = rc == 0 && count == 1 && rows[0].defs_json && rows[0].surface_sha;
if (ok) {
*json = strdup(rows[0].defs_json);
*sha = strdup(rows[0].surface_sha);
}
cbm_store_free_lsp_surfaces(rows, count);
return ok;
}
TEST(scope_facts_surface_round_trip) {
CBMFileResult *r = sf_extract(SF_PROTO, CBM_LANG_PROTOBUF, "proto/a.proto");
ASSERT_NOT_NULL(r);
char *json = NULL;
char *sha = NULL;
ASSERT_TRUE(sf_row(r, "proto/a.proto", CBM_LANG_PROTOBUF, &json, &sha));
CBMArena arena;
cbm_arena_init(&arena);
cbm_lsp_scope_facts_t facts;
ASSERT_EQ(cbm_lsp_surface_scope_from_json(&arena, json, &facts), 1);
/* every stored event comes back: all but the references, in order */
int stored = 0;
int rank = -1;
uint32_t last = 0;
for (int i = 0; i < r->scope.count; i++) {
const CBMScopeEvent *e = &r->scope.items[i];
if (e->kind == CBM_SCOPE_QREF) {
continue;
}
if (rank < 0 || e->pos != last) {
rank++;
last = e->pos;
}
ASSERT_TRUE(stored < facts.event_count);
const CBMScopeEvent *g = &facts.events[stored++];
ASSERT_EQ(g->kind, e->kind);
ASSERT_EQ(g->flags, e->flags);
ASSERT_EQ(g->pos, (uint32_t)rank);
if (e->kind == CBM_SCOPE_DEF) {
ASSERT_STR_EQ(g->name, e->name);
ASSERT_STR_EQ(g->qual, e->qual);
ASSERT_STR_EQ(facts.defs[g->index].qualified_name,
r->defs.items[e->index].qualified_name);
}
if (e->kind == CBM_SCOPE_INCLUDE) {
ASSERT_STR_EQ(facts.modules[g->index], sf_module(r, e));
}
}
ASSERT_EQ(stored, facts.event_count);
ASSERT_EQ(facts.def_count, sf_count(r, CBM_SCOPE_DEF));
ASSERT_EQ(facts.module_count, 3);
/* a language without a row stores no "is" and reads back as none */
CBMFileResult *go = sf_extract("package main\n\nfunc main() {}\n", CBM_LANG_GO, "main.go");
char *go_json = NULL;
char *go_sha = NULL;
ASSERT_TRUE(sf_row(go, "main.go", CBM_LANG_GO, &go_json, &go_sha));
ASSERT_NULL(strstr(go_json, "\"is\""));
ASSERT_EQ(cbm_lsp_surface_scope_from_json(&arena, go_json, &facts), 0);
ASSERT_EQ(
cbm_lsp_surface_scope_from_json(&arena, "{\"is\":{\"v\":99,\"ev\":[],\"im\":[]}}", &facts),
-1);
cbm_arena_destroy(&arena);
free(json);
free(sha);
free(go_json);
free(go_sha);
cbm_free_result(go);
cbm_free_result(r);
PASS();
}
/* Bytes that move keep the stored facts and so the surface hash; a new
* import changes both. */
TEST(scope_facts_surface_hash_follows_facts_not_bytes) {
char moved[sizeof(SF_PROTO) + 64];
snprintf(moved, sizeof(moved), "// a comment that moves every byte\n\n%s", SF_PROTO);
char added[sizeof(SF_PROTO) + 64];
snprintf(added, sizeof(added), "%simport \"x/more.proto\";\n", SF_PROTO);
const char *srcs[] = {SF_PROTO, moved, added};
char *json[3] = {NULL, NULL, NULL};
char *sha[3] = {NULL, NULL, NULL};
for (int i = 0; i < 3; i++) {
CBMFileResult *r = sf_extract(srcs[i], CBM_LANG_PROTOBUF, "proto/a.proto");
ASSERT_NOT_NULL(r);
ASSERT_TRUE(sf_row(r, "proto/a.proto", CBM_LANG_PROTOBUF, &json[i], &sha[i]));
cbm_free_result(r);
}
ASSERT_STR_EQ(sha[1], sha[0]);
ASSERT_STR_NEQ(sha[2], sha[0]);
for (int i = 0; i < 3; i++) {
free(json[i]);
free(sha[i]);
}
PASS();
}
/* Counted work (nodes visited plus index probes) of one file with n
* messages, n imports and n references. */
static uint64_t sf_work(int n) {
size_t cap = (size_t)n * 96 + 64;
char *src = malloc(cap);
if (!src) {
return 0;
}
size_t off = (size_t)snprintf(src, cap, "syntax = \"proto3\";\npackage g;\n");
for (int i = 0; i < n; i++) {
off += (size_t)snprintf(src + off, cap - off, "import \"d/f%d.proto\";\n", i);
}
for (int i = 0; i < n; i++) {
off += (size_t)snprintf(src + off, cap - off, "message M%d { other.T%d f = 1; }\n", i, i);
}
uint64_t before = cbm_extract_scope_nodes_visited();
CBMFileResult *r = sf_extract(src, CBM_LANG_PROTOBUF, "g.proto");
uint64_t work = cbm_extract_scope_nodes_visited() - before;
bool complete = r && sf_count(r, CBM_SCOPE_DEF) == n && sf_count(r, CBM_SCOPE_INCLUDE) == n &&
sf_count(r, CBM_SCOPE_QREF) == n;
if (r) {
cbm_free_result(r);
}
free(src);
return complete ? work : 0;
}
TEST(scope_facts_work_grows_linearly) {
uint64_t w1 = sf_work(100);
uint64_t w4 = sf_work(400);
uint64_t w16 = sf_work(1600);
ASSERT_GT(w1, 0);
ASSERT_GT(w4, 0);
ASSERT_GT(w16, 0);
/* linear up to the index probes' logarithm: at most 4.5 times per 4 times */
ASSERT_TRUE(w4 * 2 <= w1 * 9);
ASSERT_TRUE(w16 * 2 <= w4 * 9);
PASS();
}
SUITE(scope_facts) {
RUN_TEST(scope_facts_proto_definitions_includes_references);
RUN_TEST(scope_facts_language_without_row_records_nothing);
RUN_TEST(scope_facts_parse_error_is_opaque);
RUN_TEST(scope_facts_survive_compaction);
RUN_TEST(scope_facts_surface_round_trip);
RUN_TEST(scope_facts_surface_hash_follows_facts_not_bytes);
RUN_TEST(scope_facts_work_grows_linearly);
}
+66
View File
@@ -0,0 +1,66 @@
/*
* test_site_bind.c -- the site hook of the shared cross-file layer
* (src/pipeline/site_bind.h).
*
* A file of a language without a row is inactive: every call and usage site
* of it gets NO_ANSWER (main's name chain decides, unchanged) and the end
* step draws no edge. Go has an engine and never gets a row of this layer,
* so these hold as rows for other languages arrive.
*/
#include "test_framework.h"
#include "../src/pipeline/site_bind.h"
#include "../src/graph_buffer/graph_buffer.h"
#include <stdint.h>
#include <string.h>
TEST(site_bind_language_without_row_answers_nothing) {
cbm_gbuf_t *gb = cbm_gbuf_new("p", "/tmp/p");
ASSERT_NOT_NULL(gb);
int64_t caller = cbm_gbuf_upsert_node(gb, "Function", "f", "p.a.f", "a.go", 1, 3, "{}");
int64_t callee = cbm_gbuf_upsert_node(gb, "Function", "g", "p.b.g", "b.go", 1, 3, "{}");
ASSERT_GT(caller, 0);
ASSERT_GT(callee, 0);
const cbm_gbuf_node_t *source = cbm_gbuf_find_by_qn(gb, "p.a.f");
ASSERT_NOT_NULL(source);
CBMCall calls[1];
memset(calls, 0, sizeof(calls));
calls[0].callee_name = "g";
calls[0].enclosing_func_qn = "p.a.f";
CBMUsage usages[1];
memset(usages, 0, sizeof(usages));
usages[0].ref_name = "g";
usages[0].enclosing_func_qn = "p.a.f";
CBMFileResult result;
memset(&result, 0, sizeof(result));
result.calls.items = calls;
result.calls.count = 1;
result.usages.items = usages;
result.usages.count = 1;
cbm_site_file_t sf;
cbm_pipeline_site_bind_begin(&sf, NULL, gb, "a.go", CBM_LANG_GO, &result);
ASSERT_FALSE(sf.active);
cbm_site_answer_t at_call = cbm_pipeline_site_bind_call(&sf, &calls[0], source);
ASSERT_EQ(at_call.verdict, CBM_SITE_NO_ANSWER);
ASSERT_NULL(at_call.res.qualified_name);
ASSERT_NULL(at_call.target);
cbm_site_answer_t at_usage = cbm_pipeline_site_bind_usage(&sf, &usages[0], source);
ASSERT_EQ(at_usage.verdict, CBM_SITE_NO_ANSWER);
ASSERT_NULL(at_usage.res.qualified_name);
ASSERT_NULL(at_usage.target);
int edges_before = cbm_gbuf_edge_count(gb);
cbm_pipeline_site_bind_end(&sf, gb);
ASSERT_EQ(cbm_gbuf_edge_count(gb), edges_before);
cbm_gbuf_free(gb);
PASS();
}
SUITE(site_bind) {
RUN_TEST(site_bind_language_without_row_answers_nothing);
}
+1 -1
View File
@@ -33,7 +33,7 @@ static cbm_test_declarations_t *tc_config(const char *records, const char *prese
static CBMFileResult *tc_extract(const char *source, CBMLanguage language, const char *path,
const cbm_test_declarations_t *config) {
return cbm_extract_file_ex_with_tests(source, (int)strlen(source), language,
"convproj", path, 0, NULL, NULL, NULL, NULL, config);
"convproj", path, 0, NULL, NULL, NULL, NULL, config, NULL);
}
static CBMFileResult *tc_legacy(const char *source, CBMLanguage language, const char *path) {
+160
View File
@@ -117,6 +117,164 @@ done:
return ni_finish(&n, inventory, result);
}
/* Discovery and the pinned inventory decide the same thing about directories
* named deploy, deployed and env: one table of paths, committed as HEAD and
* materialized as the pinned tree, then discovered on that tree and classified
* from the inventory. A file is kept by the inventory when the ledger gives it
* a language. The two must keep exactly the same files, and the expected
* column pins the decision itself, so a wrong shared rule cannot pass by
* agreeing with itself. */
typedef struct {
const char *path;
const char *content;
bool kept;
} tic_policy_t;
/* At most 15 directories with the root: the native fixture's pinned tree allows 16. */
static const tic_policy_t TIC_POLICY[] = {
{".cbmignore", "!deployed/\n", false},
{"deploy/bootstrap", "#!/bin/sh\nkubectl apply -f k8s\n", true}, /* shell by its shebang */
{"deploy/chart/Chart.yaml", "apiVersion: v2\nname: web\n", true},
{"deploy/chart/hook.py", "def hook():\n pass\n", false},
{"deploy/k8s/app.yaml", "apiVersion: v1\nkind: ConfigMap\n", true},
{"deploy/main.go", "package deploy\n", false},
{"deploy/run", "#!/usr/bin/env python3\nprint(1)\n", false}, /* Python by its shebang */
/* `!deployed/` in .cbmignore lifts the rule for that directory, as it
* un-skipped any built-in skip directory before */
{"deployed/Dockerfile", "FROM alpine:3.20\n", true},
{"deployed/tool.rb", "class Tool; end\n", true},
{"env/pyvenv.cfg", "home = /usr/bin\n", false},
{"env/site.py", "x = 1\n", false},
{"main.go", "package main\n", true},
{"pkg/env/env.go", "package env\n", true},
{"src/main/java/deploy/Tool.java", "package deploy;\nclass Tool {}\n", true},
{"x/env/Scripts/activate", "# activate\n", false},
{"x/env/Scripts/x.py", "x = 1\n", false},
{"x/tool.py", "x = 1\n", true},
};
/* Commits `rows` as HEAD, classifies the inventory into *ledger and discovers
* the pinned tree. Counts the rows the two keep differently and the rows
* either keeps against the expected column (.cbmignore is not judged). */
static bool tic_policy_compare(if_native *n, const tic_policy_t *rows, size_t nrows,
cbm_test_impact_inventory_t **inventory,
cbm_test_impact_ledger_t **ledger, int *disagreements,
int *unexpected) {
enum { TIC_POLICY_MAX = 24 };
tic_file_t table[TIC_POLICY_MAX];
if (nrows > TIC_POLICY_MAX) {
return false;
}
for (size_t i = 0; i < nrows; i++) {
table[i] = (tic_file_t){rows[i].path, rows[i].content};
}
cbm_inventory_error_t error;
if (!tic_snapshot(n, table, nrows, inventory) ||
cbm_test_impact_classify(*inventory, n->input.tree, NULL, &n->input.control, ledger,
&error) != CBM_INVENTORY_OK) {
return false;
}
const cbm_pinned_tree_view_t *tree = cbm_pinned_tree_view(n->input.tree);
cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL};
cbm_file_info_t *files = NULL;
int count = 0;
if (!tree || !tree->root || cbm_discover(tree->root, &opts, &files, &count) != 0) {
return false;
}
*disagreements = 0;
*unexpected = 0;
for (size_t i = 0; i < nrows; i++) {
const cbm_test_impact_classified_t *row = tic_row(*ledger, n, rows[i].path);
bool by_inventory = row && row->language != CBM_LANG_COUNT;
bool by_discovery = false;
for (int f = 0; f < count; f++) {
by_discovery = by_discovery || strcmp(files[f].rel_path, rows[i].path) == 0;
}
if (by_inventory != by_discovery) {
fprintf(stderr, " %s: inventory %d, discovery %d\n", rows[i].path, (int)by_inventory,
(int)by_discovery);
(*disagreements)++;
}
if (strcmp(rows[i].path, ".cbmignore") != 0 && by_discovery != rows[i].kept) {
fprintf(stderr, " %s: kept %d, expected %d\n", rows[i].path, (int)by_discovery,
(int)rows[i].kept);
(*unexpected)++;
}
}
cbm_discover_free(files, count);
return true;
}
TEST(test_impact_classify_matches_discovery_in_deploy_and_env) {
if_native n = {0};
cbm_test_impact_inventory_t *inventory = NULL;
cbm_test_impact_ledger_t *ledger = NULL;
int result = 1;
int disagreements = -1;
int unexpected = -1;
NI_CHECK(tic_policy_compare(&n, TIC_POLICY, sizeof(TIC_POLICY) / sizeof(TIC_POLICY[0]),
&inventory, &ledger, &disagreements, &unexpected));
NI_CHECK(disagreements == 0);
NI_CHECK(unexpected == 0);
/* How each was dropped: program source in deploy/ was reached and
* classified (a ledger row with no language), a virtual environment is an
* excluded subtree (no row). */
const char *reached[] = {"deploy/chart/hook.py", "deploy/main.go", "deploy/run"};
for (size_t i = 0; i < sizeof(reached) / sizeof(reached[0]); i++) {
const cbm_test_impact_classified_t *row = tic_row(ledger, &n, reached[i]);
NI_CHECK(row && row->language == CBM_LANG_COUNT);
}
const cbm_test_impact_classified_t *yaml = tic_row(ledger, &n, "deploy/k8s/app.yaml");
const cbm_test_impact_classified_t *script = tic_row(ledger, &n, "deploy/bootstrap");
NI_CHECK(yaml && yaml->language == CBM_LANG_YAML);
NI_CHECK(script && script->language == CBM_LANG_BASH);
NI_CHECK(tic_row(ledger, &n, "env/site.py") == NULL);
NI_CHECK(tic_row(ledger, &n, "x/env/Scripts/x.py") == NULL);
result = 0;
done:
cbm_test_impact_ledger_free(ledger);
return ni_finish(&n, inventory, result);
}
/* Python environment layouts, decided the same by both: a conda environment
* (conda-meta/history) is skipped, and a marker is its exact stored name, so
* one that differs only in letter case makes no virtual environment, on any
* platform and whatever the file system's case rules. */
static const tic_policy_t TIC_ENV_LAYOUTS[] = {
{"a/env/conda-meta/history", "==> 2026-11-18 <==\n", false},
{"a/env/x.py", "x = 1\n", false},
{"b/env/PYVENV.CFG", "home = /usr/bin\n", false},
{"b/env/b.py", "x = 1\n", true},
{"c/env/c.py", "x = 1\n", true},
{"c/env/scripts/activate", "# activate\n", false},
{"d/env/bin/activate", "# activate\n", false},
{"d/env/d.py", "x = 1\n", false},
{"e/env/bin/Activate", "# activate\n", false},
{"e/env/e.py", "x = 1\n", true},
};
TEST(test_impact_classify_matches_discovery_on_env_layouts) {
if_native n = {0};
cbm_test_impact_inventory_t *inventory = NULL;
cbm_test_impact_ledger_t *ledger = NULL;
int result = 1;
int disagreements = -1;
int unexpected = -1;
NI_CHECK(tic_policy_compare(&n, TIC_ENV_LAYOUTS,
sizeof(TIC_ENV_LAYOUTS) / sizeof(TIC_ENV_LAYOUTS[0]), &inventory,
&ledger, &disagreements, &unexpected));
NI_CHECK(disagreements == 0);
NI_CHECK(unexpected == 0);
/* the kept ones were classified, the skipped ones are excluded subtrees */
NI_CHECK(tic_row(ledger, &n, "b/env/b.py") != NULL);
NI_CHECK(tic_row(ledger, &n, "a/env/x.py") == NULL);
NI_CHECK(tic_row(ledger, &n, "d/env/d.py") == NULL);
result = 0;
done:
cbm_test_impact_ledger_free(ledger);
return ni_finish(&n, inventory, result);
}
/* Reads and the probe length are charged to what the inventory's limits leave
* after the filter. Running out is a LIMIT for the whole pass, with no ledger,
* and never a default language for the files that were not read. */
@@ -381,6 +539,8 @@ SUITE(test_impact_classify) {
RUN_TEST(test_impact_registry_reads_the_snapshot_tests);
RUN_TEST(test_impact_registry_names_nothing_it_cannot_read);
RUN_TEST(test_impact_classify_gives_discovery_languages);
RUN_TEST(test_impact_classify_matches_discovery_in_deploy_and_env);
RUN_TEST(test_impact_classify_matches_discovery_on_env_layouts);
RUN_TEST(test_impact_classify_limits_are_errors_not_defaults);
RUN_TEST(test_impact_classify_uses_the_given_config);
RUN_TEST(test_impact_classify_read_failure_is_an_error);
+513
View File
@@ -392,6 +392,508 @@ TEST(type_scope_python_unlisted_bindings_get_none) {
PASS();
}
/* ── The engine table of the cross-file pass ─────────────────────── */
/* Every language that had a cross-file walk has exactly one engine row, and
* no other language has one; the engines that built a shared registry with
* the others still do, in the same order, and only they; an engine reads
* identities exactly where the step has a description of its language. */
TEST(type_scope_engine_table_rows) {
static const CBMLanguage served[] = {
CBM_LANG_GO, CBM_LANG_C, CBM_LANG_CPP, CBM_LANG_CUDA, CBM_LANG_PYTHON,
CBM_LANG_JAVASCRIPT, CBM_LANG_TYPESCRIPT, CBM_LANG_TSX, CBM_LANG_PHP, CBM_LANG_CSHARP,
CBM_LANG_JAVA, CBM_LANG_KOTLIN, CBM_LANG_RUST};
int served_count = 0;
for (int lang = 0; lang < CBM_LANG_COUNT; lang++) {
bool expect = false;
for (size_t i = 0; i < sizeof(served) / sizeof(served[0]); i++) {
expect = expect || served[i] == (CBMLanguage)lang;
}
served_count += expect ? 1 : 0;
ASSERT_EQ(cbm_pxc_has_cross_lsp((CBMLanguage)lang), expect);
ASSERT_EQ(cbm_pxc_engine_of_lang((CBMLanguage)lang) >= 0, expect);
ASSERT_EQ(cbm_pxc_wants_identities((CBMLanguage)lang),
cbm_type_scope_lang((CBMLanguage)lang) != NULL);
}
ASSERT_EQ(served_count, 13);
ASSERT(cbm_pxc_wants_identities(CBM_LANG_GO));
ASSERT(cbm_pxc_wants_identities(CBM_LANG_PYTHON));
/* One row, one registry: C, C++ and CUDA share one, as do JavaScript,
* TypeScript and TSX; Java and Kotlin do not. */
int c_row = cbm_pxc_engine_of_lang(CBM_LANG_C);
int js_row = cbm_pxc_engine_of_lang(CBM_LANG_JAVASCRIPT);
ASSERT_EQ(cbm_pxc_engine_of_lang(CBM_LANG_CPP), c_row);
ASSERT_EQ(cbm_pxc_engine_of_lang(CBM_LANG_CUDA), c_row);
ASSERT_EQ(cbm_pxc_engine_of_lang(CBM_LANG_TYPESCRIPT), js_row);
ASSERT_EQ(cbm_pxc_engine_of_lang(CBM_LANG_TSX), js_row);
ASSERT_NEQ(cbm_pxc_engine_of_lang(CBM_LANG_JAVA), cbm_pxc_engine_of_lang(CBM_LANG_KOTLIN));
ASSERT_LTE(cbm_pxc_engine_count(), CBM_PXC_MAX_ENGINES);
ASSERT_NULL(cbm_pxc_engine_name(cbm_pxc_engine_count()));
CBMArena arena;
cbm_arena_init(&arena);
CBMCrossLspRegistries regs;
memset(&regs, 0, sizeof(regs));
char built[128] = "";
for (int e = 0; e < cbm_pxc_engine_count(); e++) {
if (cbm_pxc_build_engine_registry(e, &arena, NULL, 0, &regs)) {
ASSERT_NOT_NULL(regs.by_engine[e]);
size_t at = strlen(built);
snprintf(built + at, sizeof(built) - at, "%s%s", at ? " " : "", cbm_pxc_engine_name(e));
} else {
ASSERT_NULL(regs.by_engine[e]);
}
}
ASSERT_STR_EQ(built, "go python c cs ts java");
ASSERT(cbm_pxc_registry_for_lang(&regs, CBM_LANG_CUDA) == regs.by_engine[c_row]);
ASSERT(cbm_pxc_registry_for_lang(&regs, CBM_LANG_TSX) == regs.by_engine[js_row]);
ASSERT_NULL(cbm_pxc_registry_for_lang(&regs, CBM_LANG_RUST)); /* lazy */
ASSERT_NULL(cbm_pxc_registry_for_lang(&regs, CBM_LANG_KOTLIN));
ASSERT_NULL(cbm_pxc_registry_for_lang(&regs, CBM_LANG_PHP));
ASSERT_NULL(cbm_pxc_registry_for_lang(&regs, CBM_LANG_RUBY));
ASSERT_NULL(cbm_pxc_registry_for_lang(NULL, CBM_LANG_GO));
cbm_arena_destroy(&arena);
PASS();
}
/* ── Row fields for the further engines (hand-made rows) ──────────── */
static const char *const ts_row_labels[] = {"Class", "Interface", NULL};
/* A row of an import-map language whose engine does not qualify its own
* module (every identity is stored), with the given extras. */
static cbm_type_scope_lang_t ts_row(void) {
cbm_type_scope_lang_t row = {.lang = CBM_LANG_JAVA,
.scope_source = CBM_TYPE_SCOPE_MODULE_IMPORTS,
.labels = ts_row_labels,
.return_types = true,
.param_types = true,
.field_types = true,
.embedded_types = true};
return row;
}
/* The type_scope of one def (qn, label, return text) under `row` in module
* p.app, with the scope's extras (visible) from `extra` when given. */
static const char *ts_row_attach(ts_unit_t *u, const cbm_type_scope_lang_t *row, const char *def_qn,
const char *label, const char *ret, const char *const *keys,
const char *const *vals, int n, const cbm_type_scope_t *extra) {
cbm_type_scope_t scope = {
.reg = u->reg, .module_qn = "p.app", .imp_keys = keys, .imp_vals = vals, .imp_count = n};
if (extra) {
scope.visible = extra->visible;
scope.visible_ud = extra->visible_ud;
}
cbm_type_scope_file_t *f = cbm_type_scope_file_begin(row, &scope);
if (!f) {
return "<no file state>";
}
CBMLSPDef def = {0};
def.qualified_name = def_qn;
def.short_name = "f";
def.label = label;
def.return_types = ret;
cbm_type_scope_attach(f, &u->arena, &def);
cbm_type_scope_file_end(f);
return def.type_scope;
}
/* open_imports: a name the own module and the named imports leave unbound is
* the one type of that name across the open packages, or nothing. */
TEST(type_scope_open_imports_one_answer_or_none) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "Widget", "p.x.Widget", "Class", false);
cbm_registry_add(u.reg, "Widget", "p.y.Widget", "Class", false);
cbm_registry_add(u.reg, "Gadget", "p.y.Gadget", "Class", false);
cbm_registry_add(u.reg, "Local", "p.x.Local", "Class", false);
cbm_registry_add(u.reg, "Thing", "p.app.Thing", "Function", false);
cbm_registry_add(u.reg, "Thing", "p.x.Thing", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.open_imports = true;
const char *keys[] = {"*", "*", "*"};
const char *vals[] = {"p.x", "p.y", "p.x"};
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Gadget", keys, vals, 3, NULL),
"Gadget=p.y.Gadget");
/* Two open packages declare Widget: none. */
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "Widget", keys, vals, 3, NULL));
/* The own module shadows the open packages: its Local, its Thing (a
* function, no type: none). */
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Local", keys, vals, 3, NULL),
"Local=p.app.Local");
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "Thing", keys, vals, 3, NULL));
/* A named import shadows them too. */
const char *nkeys[] = {"*", "*", "Widget"};
const char *nvals[] = {"p.x", "p.y", "p.x.Widget"};
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Widget", nkeys, nvals, 3, NULL),
"Widget=p.x.Widget");
/* Without the flag "*" is an ordinary local name: Gadget is unbound. */
row.open_imports = false;
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "Gadget", keys, vals, 3, NULL));
ts_unit_end(&u);
PASS();
}
static bool ts_implicit_string(const char *name, size_t len) {
return len == 6 && memcmp(name, "String", 6) == 0;
}
/* implicit_external: a name the language's implicit external scope declares
* (java.lang.String) is no project type of an open package. */
TEST(type_scope_implicit_external_scope_gets_none) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "String", "p.y.String", "Class", false);
cbm_registry_add(u.reg, "Gadget", "p.y.Gadget", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.open_imports = true;
const char *keys[] = {"*"};
const char *vals[] = {"p.y"};
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "String", keys, vals, 1, NULL),
"String=p.y.String");
row.implicit_external = ts_implicit_string;
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "String", keys, vals, 1, NULL));
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Gadget", keys, vals, 1, NULL),
"Gadget=p.y.Gadget");
/* The own module's String still shadows the implicit one. */
cbm_registry_add(u.reg, "String", "p.app.String", "Class", false);
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "String", keys, vals, 1, NULL),
"String=p.app.String");
ts_unit_end(&u);
PASS();
}
/* nested_types: `Inner` written inside `Outer` is `Outer.Inner`, innermost
* enclosing type first, before the module. */
TEST(type_scope_nested_types_innermost_first) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "Outer", "p.app.Outer", "Class", false);
cbm_registry_add(u.reg, "Inner", "p.app.Outer.Inner", "Class", false);
cbm_registry_add(u.reg, "Leaf", "p.app.Outer.Inner.Leaf", "Class", false);
cbm_registry_add(u.reg, "Deep", "p.app.Outer.Deep", "Class", false);
cbm_registry_add(u.reg, "Inner", "p.app.Outer.Deep.Inner", "Class", false);
cbm_registry_add(u.reg, "Inner", "p.app.Inner", "Class", false);
cbm_registry_add(u.reg, "Gizmo", "p.app.Outer.Gizmo", "Method", false);
cbm_registry_add(u.reg, "Gizmo", "p.app.Gizmo", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.nested_types = true;
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.Outer.m", "Method", "Inner", NULL, NULL, 0, NULL),
"Inner=p.app.Outer.Inner");
ASSERT_STR_EQ(
ts_row_attach(&u, &row, "p.app.Outer.Deep.n", "Method", "Inner", NULL, NULL, 0, NULL),
"Inner=p.app.Outer.Deep.Inner");
/* A type's own texts are written inside it. */
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.Outer", "Class", "Inner", NULL, NULL, 0, NULL),
"Inner=p.app.Outer.Inner");
ASSERT_STR_EQ(
ts_row_attach(&u, &row, "p.app.Outer.m", "Method", "Inner.Leaf", NULL, NULL, 0, NULL),
"Inner.Leaf=p.app.Outer.Inner.Leaf");
/* At module level, the module's. */
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Inner", NULL, NULL, 0, NULL),
"Inner=p.app.Inner");
/* The enclosing type declares Gizmo as a method: the module's Gizmo is
* hidden, and a method is no type: nothing. */
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.Outer.m", "Method", "Gizmo", NULL, NULL, 0, NULL));
/* Without the flag the module answers. */
row.nested_types = false;
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.Outer.m", "Method", "Inner", NULL, NULL, 0, NULL),
"Inner=p.app.Inner");
ts_unit_end(&u);
PASS();
}
static const char *ts_visible_widget(void *ud, const char *name, size_t len) {
(void)ud;
return len == 6 && memcmp(name, "Widget", 6) == 0 ? "p.h.Widget" : NULL;
}
/* scope_source INCLUDE_CLOSURE: the caller's closure answers the head. */
TEST(type_scope_include_closure_source) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "Widget", "p.h.Widget", "Class", false);
cbm_registry_add(u.reg, "Part", "p.h.Widget.Part", "Class", false);
cbm_registry_add(u.reg, "Gadget", "p.app.Gadget", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.scope_source = CBM_TYPE_SCOPE_INCLUDE_CLOSURE;
cbm_type_scope_t extra = {.visible = ts_visible_widget};
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Widget *", NULL, NULL, 0, &extra),
"Widget=p.h.Widget");
ASSERT_STR_EQ(
ts_row_attach(&u, &row, "p.app.f", "Function", "Widget.Part", NULL, NULL, 0, &extra),
"Widget.Part=p.h.Widget.Part");
/* Only the closure answers: the own module's Gadget is not visible by
* module, and without a closure nothing is. */
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "Gadget", NULL, NULL, 0, &extra));
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "Widget", NULL, NULL, 0, NULL));
ts_unit_end(&u);
PASS();
}
/* scope_source LEXICAL_NESTING with `::` paths (Ruby constants): the
* namespaces enclosing the def, innermost first, then the file's top level;
* `::X` and a name nothing nests get none. */
TEST(type_scope_lexical_nesting_source) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "A", "p.app.A", "Class", false);
cbm_registry_add(u.reg, "B", "p.app.A.B", "Class", false);
cbm_registry_add(u.reg, "X", "p.app.A.X", "Class", false);
cbm_registry_add(u.reg, "X", "p.app.X", "Class", false);
cbm_registry_add(u.reg, "Y", "p.app.A.B.Y", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.scope_source = CBM_TYPE_SCOPE_LEXICAL_NESTING;
row.colon_paths = true;
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.A.B.m", "Method", "X", NULL, NULL, 0, NULL),
"X=p.app.A.X");
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.A.B.m", "Method", "Y", NULL, NULL, 0, NULL),
"Y=p.app.A.B.Y");
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.A.B.m", "Method", "A::X", NULL, NULL, 0, NULL),
"A::X=p.app.A.X");
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "X", NULL, NULL, 0, NULL),
"X=p.app.X");
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.A.B.m", "Method", "::X", NULL, NULL, 0, NULL));
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.A.B.m", "Method", "Z", NULL, NULL, 0, NULL));
/* Outside a definition (no nesting) the row answers nothing. */
ts_facts_t empty;
cbm_type_scope_t scope = {
.reg = u.reg, .module_qn = "p.app", .module_facts = ts_facts(&empty, NULL, 0)};
cbm_type_scope_file_t *f = cbm_type_scope_file_begin(&row, &scope);
ASSERT_NOT_NULL(f);
ASSERT_NULL(cbm_type_scope_resolve(f, "X", 1));
cbm_type_scope_file_end(f);
ts_unit_end(&u);
PASS();
}
/* colon_paths: `ns::T` is one spelling, its QN joined with `.`; without the
* flag `ns` and `T` are two names. */
TEST(type_scope_colon_paths) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "T", "p.app.ns.T", "Class", false);
cbm_registry_add(u.reg, "vector", "p.app.std.vector", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.colon_paths = true;
ASSERT_STR_EQ(
ts_row_attach(&u, &row, "p.app.f", "Function", "std::vector<ns::T>", NULL, NULL, 0, NULL),
"std::vector=p.app.std.vector|ns::T=p.app.ns.T");
row.colon_paths = false;
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "ns::T", NULL, NULL, 0, NULL));
ts_unit_end(&u);
PASS();
}
/* The calling file's own table: each local its named imports bind, with the
* type identity it resolves to; Go binds packages (none); the open key and
* a non-type are left out. */
TEST(type_scope_calling_file_imports) {
ts_unit_t u;
ts_unit_begin(&u);
ts_facts_t facts;
const char *keys[] = {"Store", "make", "Store", "Other", "Local"};
const char *vals[] = {"p.a.Store", "p.c.make", "p.a.Store", "p.z.Other", "p.app.Local"};
const char *bound[] = {"Store", "make", "Other", "Local"};
cbm_type_scope_t scope = {.reg = u.reg,
.module_qn = "p.app",
.imp_keys = keys,
.imp_vals = vals,
.imp_count = 5,
.module_facts = ts_facts(&facts, bound, 4)};
cbm_type_scope_file_t *f =
cbm_type_scope_file_begin(cbm_type_scope_lang(CBM_LANG_PYTHON), &scope);
ASSERT_NOT_NULL(f);
const char *const *locals = NULL;
const char *const *qns = NULL;
ASSERT_EQ(cbm_type_scope_file_imports(f, &locals, &qns), 2);
ASSERT_STR_EQ(locals[0], "Store");
ASSERT_STR_EQ(qns[0], "p.a.Store");
ASSERT_STR_EQ(locals[1], "Local");
ASSERT_STR_EQ(qns[1], "p.app.Local");
/* Built once: the same arrays again. */
const char *const *again = NULL;
ASSERT_EQ(cbm_type_scope_file_imports(f, &again, NULL), 2);
ASSERT(again == locals);
cbm_type_scope_file_end(f);
/* A module whose bindings cannot be listed (a star import) has none. */
facts.items[4] = (CBMPyNamespaceFact){.kind = CBM_PY_NS_IMPORT_STAR};
facts.facts.count = 5;
f = cbm_type_scope_file_begin(cbm_type_scope_lang(CBM_LANG_PYTHON), &scope);
ASSERT_NOT_NULL(f);
ASSERT_EQ(cbm_type_scope_file_imports(f, &locals, &qns), 0);
cbm_type_scope_file_end(f);
const char *gkeys[] = {"a"};
const char *gvals[] = {"p.a"};
cbm_type_scope_t go = {
.reg = u.reg, .module_qn = "p.app", .imp_keys = gkeys, .imp_vals = gvals, .imp_count = 1};
f = cbm_type_scope_file_begin(cbm_type_scope_lang(CBM_LANG_GO), &go);
ASSERT_NOT_NULL(f);
ASSERT_EQ(cbm_type_scope_file_imports(f, &locals, &qns), 0);
cbm_type_scope_file_end(f);
ASSERT_EQ(cbm_type_scope_file_imports(NULL, &locals, &qns), 0);
ASSERT_NULL(locals);
/* An open row leaves its open key out. */
cbm_type_scope_lang_t row = ts_row();
row.open_imports = true;
const char *okeys[] = {"*", "Store"};
const char *ovals[] = {"p.a", "p.a.Store"};
cbm_type_scope_t open = {
.reg = u.reg, .module_qn = "p.app", .imp_keys = okeys, .imp_vals = ovals, .imp_count = 2};
f = cbm_type_scope_file_begin(&row, &open);
ASSERT_NOT_NULL(f);
ASSERT_EQ(cbm_type_scope_file_imports(f, &locals, &qns), 1);
ASSERT_STR_EQ(locals[0], "Store");
cbm_type_scope_file_end(f);
ts_unit_end(&u);
PASS();
}
/* A walk reads its calling file's table from the same scope the collector
* attaches with: none without a context or for an engine that reads no
* identities. */
TEST(type_scope_calling_file_scope_per_engine) {
ts_unit_t u;
ts_unit_begin(&u);
ts_facts_t facts;
const char *keys[] = {"Store"};
const char *vals[] = {"p.a.Store"};
CBMFileResult *fr = calloc(1, sizeof(*fr));
ASSERT_NOT_NULL(fr);
fr->py_namespace = *ts_facts(&facts, keys, 1);
cbm_pipeline_ctx_t *ctx = calloc(1, sizeof(*ctx));
ASSERT_NOT_NULL(ctx);
ctx->registry = u.reg;
cbm_type_scope_file_t *f = cbm_pxc_file_scope(ctx, CBM_LANG_PYTHON, fr, "p.app", keys, vals, 1);
ASSERT_NOT_NULL(f);
const char *const *locals = NULL;
const char *const *qns = NULL;
ASSERT_EQ(cbm_type_scope_file_imports(f, &locals, &qns), 1);
ASSERT_STR_EQ(qns[0], "p.a.Store");
cbm_type_scope_file_end(f);
ASSERT_NULL(cbm_pxc_file_scope(NULL, CBM_LANG_PYTHON, fr, "p.app", keys, vals, 1));
ASSERT_NULL(cbm_pxc_file_scope(ctx, CBM_LANG_CPP, fr, "p.app", keys, vals, 1));
ASSERT_NULL(cbm_pxc_file_scope(ctx, CBM_LANG_JAVA, fr, "p.app", keys, vals, 1));
free(ctx);
free(fr);
ts_unit_end(&u);
PASS();
}
/* Counted work of the step for `defs` methods four types deep, each writing
* `names` member-type names (nested_types), or the same names through
* `open` open packages (open_imports). */
static uint64_t ts_row_work(int defs, int names, int open, bool nested) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "N0", "p.app.N0", "Class", false);
cbm_registry_add(u.reg, "N1", "p.app.N0.N1", "Class", false);
cbm_registry_add(u.reg, "N2", "p.app.N0.N1.N2", "Class", false);
cbm_registry_add(u.reg, "N3", "p.app.N0.N1.N2.N3", "Class", false);
char buf[64];
size_t text_cap = (size_t)names * 16 + 1;
char *text = calloc(1, text_cap);
const char *keys[16];
const char *vals[16];
char owned[16][16];
open = open < 16 ? open : 16;
for (int i = 0; i < names; i++) {
snprintf(buf, sizeof(buf), nested ? "p.app.N0.M%d" : "p.o0.M%d", i);
cbm_registry_add(u.reg, "M", buf, "Class", false);
size_t at = strlen(text);
snprintf(text + at, text_cap - at, "%sM%d", i ? "|" : "", i);
}
for (int i = 0; i < open; i++) {
snprintf(owned[i], sizeof(owned[i]), "p.o%d", i);
keys[i] = "*";
vals[i] = owned[i];
}
cbm_type_scope_lang_t row = ts_row();
row.nested_types = nested;
row.open_imports = !nested;
cbm_type_scope_t scope = {
.reg = u.reg, .module_qn = "p.app", .imp_keys = keys, .imp_vals = vals, .imp_count = open};
cbm_type_scope_file_t *f = cbm_type_scope_file_begin(&row, &scope);
uint64_t before = cbm_type_scope_test_work();
for (int d = 0; f && d < defs; d++) {
snprintf(buf, sizeof(buf), "p.app.N0.N1.N2.N3.m%d", d);
CBMLSPDef def = {.qualified_name = buf, .short_name = "m", .label = "Method"};
def.return_types = text;
cbm_type_scope_attach(f, &u.arena, &def);
}
uint64_t work = cbm_type_scope_test_work() - before;
cbm_type_scope_file_end(f);
free(text);
ts_unit_end(&u);
return work;
}
TEST(type_scope_nested_and_open_work_grow_linearly) {
struct {
const char *what;
uint64_t small;
uint64_t large;
} cases[] = {
{"nested, defs", ts_row_work(500, 40, 0, true), ts_row_work(1000, 40, 0, true)},
{"nested, names", ts_row_work(50, 400, 0, true), ts_row_work(50, 800, 0, true)},
{"open, defs", ts_row_work(500, 40, 8, false), ts_row_work(1000, 40, 8, false)},
{"open, names", ts_row_work(50, 400, 8, false), ts_row_work(50, 800, 8, false)},
};
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
double ratio = (double)cases[i].large / (double)(cases[i].small ? cases[i].small : 1);
printf(" %s: %llu -> %llu (x%.2f)\n", cases[i].what, (unsigned long long)cases[i].small,
(unsigned long long)cases[i].large, ratio);
ASSERT_GT(cases[i].small, 0);
ASSERT(ratio > 1.7 && ratio < 2.3);
}
PASS();
}
/* The fold's rows keep each engine's present case: Go and C fold their
* structs' fields in declaration order; CUDA files fold none. */
static const char *ts_folded_fields(CBMLanguage lang, const char *rel, const char *src,
const char *owner_label, char *out, size_t cap) {
out[0] = '\0';
CBMFileResult *r = cbm_extract_file(src, (int)strlen(src), lang, "p", rel, 0, NULL, NULL);
if (!r) {
return "<no result>";
}
CBMFileResult *cache[] = {r};
char rel_path[32];
snprintf(rel_path, sizeof(rel_path), "%s", rel);
cbm_file_info_t files[] = {{.rel_path = rel_path, .language = lang}};
char *modules[1] = {NULL};
int count = 0;
CBMArena arena;
cbm_arena_init(&arena);
CBMLSPDef *defs =
cbm_pxc_collect_all_defs(NULL, &arena, cache, files, 1, "p", modules, &count, NULL);
for (int i = 0; defs && i < count; i++) {
if (defs[i].label && strcmp(defs[i].label, owner_label) == 0) {
snprintf(out, cap, "%s", defs[i].field_defs ? defs[i].field_defs : "(none)");
break;
}
}
free(defs);
free(modules[0]);
cbm_arena_destroy(&arena);
cbm_free_result(r);
return out;
}
TEST(type_scope_field_fold_rows) {
char got[128];
ASSERT_STR_EQ(ts_folded_fields(CBM_LANG_GO, "a.go",
"package p\ntype T struct {\n\tA int\n\tB *T\n}\n", "Struct",
got, sizeof(got)),
"A:int|B:*T");
const char *c_src = "struct point { int x; double y; };\n";
ASSERT_STR_EQ(ts_folded_fields(CBM_LANG_C, "a.c", c_src, "Class", got, sizeof(got)),
"x:int|y:double");
ASSERT_STR_EQ(ts_folded_fields(CBM_LANG_CUDA, "a.cu", c_src, "Class", got, sizeof(got)),
"(none)");
PASS();
}
/* ── Growth: counted work at two sizes ─────────────────────────────── */
static bool ts_linear(uint64_t small, uint64_t large) {
@@ -2415,6 +2917,17 @@ SUITE(type_scope) {
RUN_TEST(type_scope_python_class_body_bindings);
RUN_TEST(type_scope_python_type_parameters);
RUN_TEST(type_scope_python_unlisted_bindings_get_none);
RUN_TEST(type_scope_engine_table_rows);
RUN_TEST(type_scope_open_imports_one_answer_or_none);
RUN_TEST(type_scope_implicit_external_scope_gets_none);
RUN_TEST(type_scope_nested_types_innermost_first);
RUN_TEST(type_scope_include_closure_source);
RUN_TEST(type_scope_lexical_nesting_source);
RUN_TEST(type_scope_colon_paths);
RUN_TEST(type_scope_calling_file_imports);
RUN_TEST(type_scope_calling_file_scope_per_engine);
RUN_TEST(type_scope_nested_and_open_work_grow_linearly);
RUN_TEST(type_scope_field_fold_rows);
RUN_TEST(type_scope_step_work_grows_linearly);
RUN_TEST(type_scope_lookup_work_grows_linearly);
RUN_TEST(type_scope_reach_work_grows_linearly);