Skip to content

Commit ea2a8f1

Browse files
committed
Merge main into fix/c-typedefs-macros-enumerators for the release batch
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2 parents c828105 + 1b581f1 commit ea2a8f1

38 files changed

Lines changed: 2570 additions & 127 deletions

‎.github/workflows/codeql.yml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ on:
99
permissions:
1010
contents: read
1111

12+
# One analysis per pull request and one per pushed branch. A newer push to main
13+
# cancels the analysis of an older main commit; only the newest commit's
14+
# results matter for main, and a merge-heavy day otherwise queues one 40-100
15+
# minute analysis per merge ahead of every pull request run.
1216
concurrency:
13-
group: codeql-${{ github.ref }}-${{ github.event.pull_request.number || github.sha }}
17+
group: codeql-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }}
1418
cancel-in-progress: true
1519

1620
jobs:

‎.github/workflows/scorecard.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ on:
88

99
permissions: read-all
1010

11+
# Only the newest main commit is scored: a newer push (or the weekly run)
12+
# cancels a pending or running score of an older commit.
13+
concurrency:
14+
group: scorecard-${{ github.ref }}
15+
cancel-in-progress: true
16+
1117
jobs:
1218
scorecard:
1319
runs-on: ubuntu-latest

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ The install script placed beside the binary is **reported, not deleted** — uni
222222
- **`CROSS_*` edges** link nodes across multiple repos indexed under the same store
223223
- **Multi-galaxy 3D UI layout** for cross-repo architecture visualization
224224
- **Cross-repo architecture summary** combining services, routes, and dependencies across the indexed fleet
225+
- **Run status**: `index_status` (and the UI layout next to `linked_projects`) carries a `cross_repo` object, `{"status":"never_run"}` until `index_repository(mode="cross-repo-intelligence")` has run with that project as the source, then `"ran"` with `last_run_at`, `outcome`, `targets`, `projects_scanned`, `total_cross_edges` and `skipped_projects`. That is how you tell "never linked" apart from "linked, nothing matched". The status is stored in the project's own index next to its `CROSS_*` edges, so a reindex that rebuilds the index drops both at once and the status goes back to `never_run`.
226+
- **Skipped stores**: `target_projects: ["*"]` skips an index built before the #768 schema change rather than failing the whole run. The response lists each one as `skipped_projects: [{"name", "reason": "pre_768_schema", "hint": "reindex this project"}]`.
225227

226228
### Edge types (selected)
227229
- `CALLS` — a callable is invoked at the source site

‎internal/cbm/cbm.c‎

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,20 +281,80 @@ void cbm_channels_push(CBMChannelArray *arr, CBMArena *a, CBMChannel ch) {
281281
typedef struct {
282282
const char *string;
283283
uint32_t length;
284+
/* #2078: serve one "\n" at offset `length` -- see cbm_parse_source. */
285+
bool virtual_newline;
284286
} CBMStringInput;
285287

286288
static const char *cbm_string_read(void *payload, uint32_t byte, TSPoint point,
287289
uint32_t *bytes_read) {
288290
(void)point;
289291
CBMStringInput *self = (CBMStringInput *)payload;
290292
if (byte >= self->length) {
293+
if (self->virtual_newline && byte == self->length) {
294+
*bytes_read = 1;
295+
return "\n";
296+
}
291297
*bytes_read = 0;
292298
return "";
293299
}
294300
*bytes_read = self->length - byte;
295301
return self->string + byte;
296302
}
297303

304+
/* Where the parser stands after reading all `len` real bytes. */
305+
static TSPoint cbm_source_end_point(const char *source, uint32_t len) {
306+
TSPoint end = {0, 0};
307+
const char *p = source;
308+
const char *stop = source + len;
309+
const char *nl;
310+
while (p < stop && (nl = memchr(p, '\n', (size_t)(stop - p))) != NULL) {
311+
end.row++;
312+
p = nl + 1;
313+
}
314+
end.column = (uint32_t)(stop - p);
315+
return end;
316+
}
317+
318+
/* #2078: parse every file as if its last line were terminated.
319+
*
320+
* Many grammars treat the line terminator as part of the construct it ends --
321+
* a markdown fence, heading or list marker, a Makefile recipe, a Dockerfile
322+
* instruction. A file whose last byte is not "\n" leaves that construct
323+
* unterminated: at best a zero-width MISSING token (#1610/#1746 excuse those at
324+
* EOF), at worst a WIDTH-BEARING error that costs the whole last line or, for a
325+
* one-line file, the whole file, and sometimes a silent loss (an unterminated
326+
* markdown heading produced no Section and no flag). The same bytes plus one
327+
* "\n" parse clean, so the absent newline is supplied instead of excused.
328+
*
329+
* The parser reads one virtual "\n" past EOF; afterwards a single tree edit
330+
* deletes it again. tree-sitter's own edit arithmetic then clamps every node
331+
* that reached into the virtual byte back to the real end -- byte offsets AND
332+
* row/column points -- so every consumer (def line ranges, node text, call
333+
* byte spans, parse-coverage ranges, the retained tree the LSP passes reuse)
334+
* sees only real bytes and real lines; nothing downstream needs to know. The
335+
* caller's buffer is never copied or written.
336+
*
337+
* A file that is empty or already ends in "\n" is parsed exactly as before. */
338+
TSTree *cbm_parse_source(TSParser *parser, const char *source, uint32_t source_len,
339+
TSParseOptions opts) {
340+
CBMStringInput input = {source, source_len, source_len > 0 && source[source_len - 1] != '\n'};
341+
TSInput ts_input = {&input, cbm_string_read, TSInputEncodingUTF8, NULL};
342+
TSTree *tree = ts_parser_parse_with_options(parser, NULL, ts_input, opts);
343+
if (tree && input.virtual_newline) {
344+
TSPoint end = cbm_source_end_point(source, source_len);
345+
TSInputEdit drop_virtual_newline = {
346+
.start_byte = source_len,
347+
.old_end_byte = source_len + 1,
348+
.new_end_byte = source_len,
349+
.start_point = end,
350+
.old_end_point = {end.row + 1, 0},
351+
.new_end_point = end,
352+
};
353+
ts_tree_edit(tree, &drop_virtual_newline);
354+
}
355+
return tree;
356+
}
357+
298358
// --- Parse timeout callback ---
299359

300360
/* Budget for the tree-sitter progress callback. The PRIMARY gate is per-thread
@@ -1264,7 +1324,11 @@ static void cbm_error_regions_push(cbm_error_regions_t *acc, TSNode n) {
12641324
* #1746: the Dockerfile grammar places that zero-width missing newline before
12651325
* trailing whitespace rather than at raw EOF. Preserve the broad exact-EOF
12661326
* rule above; only extend it past blanks when the missing token is specifically
1267-
* a newline. */
1327+
* a newline.
1328+
*
1329+
* #2078: cbm_parse_source now supplies the absent final newline, so a MISSING
1330+
* newline at EOF no longer arises from it; the rule stays for the other
1331+
* zero-width terminators a grammar can leave at EOF. */
12681332
static bool cbm_is_blank_not_newline(char c) {
12691333
return c == ' ' || c == '\t' || c == '\v' || c == '\f' || c == '\r';
12701334
}
@@ -2661,8 +2725,6 @@ static void extract_cpp_branch_views(const CBMExtractCtx *raw, const TSLanguage
26612725
return;
26622726
}
26632727
ts_parser_reset(parser);
2664-
CBMStringInput input = {view, (uint32_t)raw->source_len};
2665-
TSInput ts_input = {&input, cbm_string_read, TSInputEncodingUTF8, NULL};
26662728
TSParseOptions opts = {0};
26672729
CBMParseBudget budget = {0}; // cppcheck-suppress unreadVariable
26682730
if (timeout_micros > 0) {
@@ -2672,7 +2734,9 @@ static void extract_cpp_branch_views(const CBMExtractCtx *raw, const TSLanguage
26722734
opts.payload = &budget;
26732735
opts.progress_callback = cbm_timeout_cb;
26742736
}
2675-
TSTree *tree = ts_parser_parse_with_options(parser, NULL, ts_input, opts);
2737+
/* A view keeps the raw source's length and line structure, so it is
2738+
* parsed under the same terminated-last-line rule (#2078). */
2739+
TSTree *tree = cbm_parse_source(parser, view, (uint32_t)raw->source_len, opts);
26762740
if (!tree) {
26772741
return;
26782742
}
@@ -2780,15 +2844,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
27802844

27812845
uint64_t t0 = now_ns();
27822846

2783-
// Build string input + timeout options for parse_with_options
2784-
CBMStringInput str_input = {source, (uint32_t)source_len};
2785-
TSInput ts_input = {
2786-
&str_input,
2787-
cbm_string_read,
2788-
TSInputEncodingUTF8,
2789-
NULL,
2790-
};
2791-
2847+
// Timeout options for parse_with_options
27922848
TSParseOptions opts = {0};
27932849
CBMParseBudget budget = {0}; // cppcheck-suppress unreadVariable
27942850
uint64_t budget_ns = 0;
@@ -2816,12 +2872,14 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
28162872
/* #1735: a SQL data dump's literal-only INSERT rows carry no graph content
28172873
* but dominate its parse. Keep them out through included ranges; offsets
28182874
* and positions of everything kept are unchanged. The parser is
2819-
* thread-local and reused, so the ranges are cleared right after. */
2875+
* thread-local and reused, so the ranges are cleared right after. The last
2876+
* range ends at the real EOF, so the virtual final newline cbm_parse_source
2877+
* supplies (#2078) stays outside a ranged parse. */
28202878
CBMSqlKeptRanges sql_kept = {NULL, 0};
28212879
bool sql_ranged = language == CBM_LANG_SQL && cbm_sql_values_exclusion_on(rel_path) &&
28222880
cbm_sql_values_kept_ranges(source, (uint32_t)source_len, &sql_kept) &&
28232881
ts_parser_set_included_ranges(parser, sql_kept.items, sql_kept.count);
2824-
TSTree *tree = ts_parser_parse_with_options(parser, NULL, ts_input, opts);
2882+
TSTree *tree = cbm_parse_source(parser, source, (uint32_t)source_len, opts);
28252883
if (sql_ranged) {
28262884
(void)ts_parser_set_included_ranges(parser, NULL, 0);
28272885
}
@@ -3042,7 +3100,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
30423100
TSParser *pp_parser = get_thread_parser(ts_lang, language);
30433101
if (pp_parser) {
30443102
ts_parser_reset(pp_parser);
3045-
CBMStringInput pp_input = {expanded, (uint32_t)expanded_len};
3103+
CBMStringInput pp_input = {expanded, (uint32_t)expanded_len, false};
30463104
TSInput pp_ts_input = {
30473105
&pp_input,
30483106
cbm_string_read,

‎internal/cbm/cbm.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,14 @@ void cbm_work_arena_keep_begin(void);
917917
/* Free the compaction scratch this thread kept (cbm_work_arena_release calls it). */
918918
void cbm_result_compact_release_thread(void);
919919

920+
/* Parse one whole file as if its last line ended with "\n" (#2078). The
921+
* parser sees the source plus one virtual newline when the last byte is not
922+
* already one; the returned tree is then clamped back to `source_len`, so no
923+
* node range, point or text reaches past the real bytes. Every whole-file parse
924+
* goes through here, so a retained tree and a fallback re-parse agree. */
925+
TSTree *cbm_parse_source(TSParser *parser, const char *source, uint32_t source_len,
926+
TSParseOptions opts);
927+
920928
// Extract all data from one file. Caller must call cbm_free_result().
921929
// source must remain valid for the duration of the call.
922930
// timeout_micros: per-file tree-sitter parse budget in microseconds of the

‎internal/cbm/extract_calls.c‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,6 +2688,123 @@ static const char *normalize_string_handler(CBMArena *a, const char *raw) {
26882688
return unq;
26892689
}
26902690

2691+
/* True for a PHP class/namespace name: identifier segments joined by '\'. */
2692+
static bool php_is_class_path(const char *s) {
2693+
if (!s || !s[0]) {
2694+
return false;
2695+
}
2696+
for (; *s; s++) {
2697+
if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || (*s >= '0' && *s <= '9') ||
2698+
*s == '_' || *s == '\\')) {
2699+
return false;
2700+
}
2701+
}
2702+
return true;
2703+
}
2704+
2705+
/* Qualify a class name the way PHP resolves it: a leading '\' is already
2706+
* absolute; a first segment that a `use` clause imports (by alias or by last
2707+
* segment) is replaced by the imported name; anything else is relative to the
2708+
* file's namespace. Imports and the namespace are extracted before the call
2709+
* walk, so both are complete here. */
2710+
static const char *php_qualify_class(CBMExtractCtx *ctx, const char *written) {
2711+
if (written[0] == '\\') {
2712+
return written + SKIP_ONE;
2713+
}
2714+
const char *sep = strchr(written, '\\');
2715+
size_t first_len = sep ? (size_t)(sep - written) : strlen(written);
2716+
const CBMImportArray *imps = &ctx->result->imports;
2717+
for (int i = 0; i < imps->count; i++) {
2718+
const CBMImport *imp = &imps->items[i];
2719+
const char *path = imp->module_path;
2720+
if (!imp->local_name || !path || strlen(imp->local_name) != first_len ||
2721+
strncmp(imp->local_name, written, first_len) != 0) {
2722+
continue;
2723+
}
2724+
path += path[0] == '\\' ? SKIP_ONE : 0;
2725+
if (php_is_class_path(path)) {
2726+
return cbm_arena_sprintf(ctx->arena, "%s%s", path, sep ? sep : "");
2727+
}
2728+
}
2729+
const char *ns = ctx->result->namespace_name;
2730+
return ns && ns[0] ? cbm_arena_sprintf(ctx->arena, "%s\\%s", ns, written) : written;
2731+
}
2732+
2733+
/* The fully-qualified class a PHP `X::class` expression names (`use` imports
2734+
* and the file namespace applied). NULL for any other expression, including
2735+
* `static::class` and `self::class`. */
2736+
static const char *php_class_literal_fqn(CBMExtractCtx *ctx, TSNode node) {
2737+
if (strcmp(ts_node_type(node), "class_constant_access_expression") != 0 ||
2738+
ts_node_named_child_count(node) != PAIR_LEN) {
2739+
return NULL;
2740+
}
2741+
TSNode cls = ts_node_named_child(node, 0);
2742+
const char *ck = ts_node_type(cls);
2743+
const char *member =
2744+
cbm_node_text(ctx->arena, ts_node_named_child(node, SKIP_ONE), ctx->source);
2745+
if ((strcmp(ck, "name") != 0 && strcmp(ck, "qualified_name") != 0) || !member ||
2746+
strcmp(member, "class") != 0) {
2747+
return NULL;
2748+
}
2749+
const char *text = cbm_node_text(ctx->arena, cls, ctx->source);
2750+
return php_is_class_path(text) ? php_qualify_class(ctx, text) : NULL;
2751+
}
2752+
2753+
/* The value of a PHP array element written without a key; a null node for a
2754+
* keyed element (`'uses' => ...`) or anything else. */
2755+
static TSNode php_unkeyed_element_value(TSNode elem) {
2756+
TSNode none = {0};
2757+
if (strcmp(ts_node_type(elem), "array_element_initializer") != 0 ||
2758+
ts_node_named_child_count(elem) != SKIP_ONE) {
2759+
return none;
2760+
}
2761+
return ts_node_named_child(elem, 0);
2762+
}
2763+
2764+
static bool php_is_identifier(const char *s) {
2765+
if (!s || !((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || *s == '_')) {
2766+
return false;
2767+
}
2768+
for (; *s; s++) {
2769+
if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || (*s >= '0' && *s <= '9') ||
2770+
*s == '_')) {
2771+
return false;
2772+
}
2773+
}
2774+
return true;
2775+
}
2776+
2777+
/* Laravel's class-based route handlers (#1146), as "Ns\Class::method":
2778+
* [UserController::class, 'show'] -> App\Http\Controllers\UserController::show
2779+
* GetCurrentUserController::class -> App\...\GetCurrentUserController::__invoke
2780+
* with the class qualified through the file's `use` imports; the route passes
2781+
* place it on a method QN (cbm_registry_resolve_handler). NULL for any other
2782+
* expression. */
2783+
static const char *php_class_route_handler(CBMExtractCtx *ctx, TSNode arg) {
2784+
const char *kind = ts_node_type(arg);
2785+
if (strcmp(kind, "class_constant_access_expression") == 0) {
2786+
const char *cls = php_class_literal_fqn(ctx, arg);
2787+
return cls ? cbm_arena_sprintf(ctx->arena, "%s::__invoke", cls) : NULL;
2788+
}
2789+
if (strcmp(kind, "array_creation_expression") != 0 ||
2790+
ts_node_named_child_count(arg) != PAIR_LEN) {
2791+
return NULL;
2792+
}
2793+
TSNode cls_node = php_unkeyed_element_value(ts_node_named_child(arg, 0));
2794+
TSNode method_node = php_unkeyed_element_value(ts_node_named_child(arg, SKIP_ONE));
2795+
if (ts_node_is_null(cls_node) || ts_node_is_null(method_node)) {
2796+
return NULL;
2797+
}
2798+
const char *cls = php_class_literal_fqn(ctx, cls_node);
2799+
const char *mk = ts_node_type(method_node);
2800+
if (!cls || (strcmp(mk, "string") != 0 && strcmp(mk, "encapsed_string") != 0)) {
2801+
return NULL;
2802+
}
2803+
const char *method =
2804+
strip_quotes(ctx->arena, cbm_node_text(ctx->arena, method_node, ctx->source));
2805+
return php_is_identifier(method) ? cbm_arena_sprintf(ctx->arena, "%s::%s", cls, method) : NULL;
2806+
}
2807+
26912808
static const char *extract_handler_arg(CBMExtractCtx *ctx, TSNode args) {
26922809
/* The LAST eligible argument wins, and every argument is examined.
26932810
* Express, Fastify, gin and Laravel all put middleware between the route
@@ -2705,6 +2822,13 @@ static const char *extract_handler_arg(CBMExtractCtx *ctx, TSNode args) {
27052822
if (strcmp(ts_node_type(arg2), "argument") == 0 && ts_node_named_child_count(arg2) > 0) {
27062823
arg2 = ts_node_named_child(arg2, 0);
27072824
}
2825+
if (ctx->language == CBM_LANG_PHP) {
2826+
const char *class_handler = php_class_route_handler(ctx, arg2);
2827+
if (class_handler) {
2828+
handler = class_handler;
2829+
continue;
2830+
}
2831+
}
27082832
const char *ak2 = ts_node_type(arg2);
27092833
/* `name` = PHP bare identifier handler; string = Laravel string handler
27102834
* ('showUsers' or 'Controller@method'). */

‎internal/cbm/lsp/c_lsp.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6777,7 +6777,7 @@ bool cbm_run_c_lsp_cross_with_registry_with_test_owners(CBMArena *arena, const c
67776777
return false;
67786778
const TSLanguage *ts_lang = cpp_mode ? tree_sitter_cpp() : tree_sitter_c();
67796779
ts_parser_set_language(parser, ts_lang);
6780-
tree = ts_parser_parse_string(parser, NULL, source, source_len);
6780+
tree = cbm_parse_source(parser, source, (uint32_t)source_len, (TSParseOptions){0});
67816781
ts_parser_delete(parser);
67826782
owns_tree = true;
67836783
if (!tree)
@@ -6847,7 +6847,7 @@ bool cbm_run_c_lsp_cross_with_test_owners(CBMArena *arena, const char *source, i
68476847
return false;
68486848
const TSLanguage *ts_lang = cpp_mode ? tree_sitter_cpp() : tree_sitter_c();
68496849
ts_parser_set_language(parser, ts_lang);
6850-
tree = ts_parser_parse_string(parser, NULL, source, source_len);
6850+
tree = cbm_parse_source(parser, source, (uint32_t)source_len, (TSParseOptions){0});
68516851
ts_parser_delete(parser);
68526852
owns_tree = true;
68536853
if (!tree)

‎internal/cbm/lsp/cs_lsp.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3763,8 +3763,9 @@ void cbm_run_cs_lsp_cross_with_registry(CBMArena *arena, const char *source, int
37633763
if (!parser)
37643764
return;
37653765
ts_parser_set_language(parser, tree_sitter_c_sharp());
3766-
tree = ts_parser_parse_string(
3767-
parser, NULL, source, source_len > 0 ? (uint32_t)source_len : (uint32_t)strlen(source));
3766+
tree = cbm_parse_source(parser, source,
3767+
source_len > 0 ? (uint32_t)source_len : (uint32_t)strlen(source),
3768+
(TSParseOptions){0});
37683769
ts_parser_delete(parser);
37693770
owns = true;
37703771
}
@@ -3805,8 +3806,9 @@ void cbm_run_cs_lsp_cross(CBMArena *arena, const char *source, int source_len,
38053806
if (!parser)
38063807
return;
38073808
ts_parser_set_language(parser, tree_sitter_c_sharp());
3808-
tree = ts_parser_parse_string(
3809-
parser, NULL, source, source_len > 0 ? (uint32_t)source_len : (uint32_t)strlen(source));
3809+
tree = cbm_parse_source(parser, source,
3810+
source_len > 0 ? (uint32_t)source_len : (uint32_t)strlen(source),
3811+
(TSParseOptions){0});
38103812
ts_parser_delete(parser);
38113813
owns = true;
38123814
}

0 commit comments

Comments
 (0)