Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Merge main into fix/issue-686-fiber-group-v2 for the release batch

Martin Vogel5e596c73d796
diff --cc internal/cbm/extract_calls.c
index 89caca047,a8b97c639..bc62393a5
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@@ -1763,50 -1763,61 +1763,103 @@@ static const char *php_chain_prefix_arg
return NULL;
}
+/* Compose route-group prefix segments into one path. parts[] is inner-first;
+ * the result is outer-first with exactly one '/' between segments and a
+ * leading '/'. Returns NULL when no segment contributes or the result would
+ * be oversized (the route then stays un-prefixed). Shared by the Laravel
+ * (#952) and Go router-group (#686) prefix walks. */
+static const char *route_prefix_compose(CBMArena *a, const char *const *parts, int part_count) {
+ char buf[CBM_SZ_256];
+ size_t pos = 0;
+ for (int i = part_count - 1; i >= 0; i--) {
+ const char *seg = parts[i];
+ while (*seg == '/') {
+ seg++;
+ }
+ size_t sl = strlen(seg);
+ if (sl == 0) {
+ continue;
+ }
+ if (pos + sl + 2 >= sizeof(buf)) {
+ return NULL; /* oversized — leave path un-prefixed */
+ }
+ buf[pos++] = '/';
+ memcpy(buf + pos, seg, sl);
+ pos += sl;
+ while (pos > 1 && buf[pos - 1] == '/') {
+ pos--; /* strip trailing slash per segment */
+ }
+ }
+ buf[pos] = '\0';
+ return pos ? cbm_arena_strndup(a, buf, pos) : NULL;
+}
+
+/* Prepend a composed group prefix to a route path ("/x" + "/users" ->
+ * "/x/users"; a bare "/" route becomes the prefix itself). */
+static const char *route_path_with_prefix(CBMArena *a, const char *prefix, const char *path) {
+ const char *rel = path;
+ while (*rel == '/') {
+ rel++;
+ }
+ return rel[0] ? cbm_arena_sprintf(a, "%s/%s", prefix, rel)
+ : cbm_arena_strndup(a, prefix, strlen(prefix));
+}
+
+ /* The value of a plain PHP string literal ('...' or "..." with no
+ * interpolation or escapes), "" for an empty one; NULL for anything else. */
+ static const char *php_plain_string_value(CBMArena *a, TSNode node, const char *source) {
+ const char *kind = ts_node_type(node);
+ if (strcmp(kind, "string") != 0 && strcmp(kind, "encapsed_string") != 0) {
+ return NULL;
+ }
+ uint32_t nc = ts_node_named_child_count(node);
+ for (uint32_t i = 0; i < nc; i++) {
+ if (strcmp(ts_node_type(ts_node_named_child(node, i)), "string_content") != 0) {
+ return NULL;
+ }
+ }
+ return nc == 0 ? "" : cbm_node_text(a, ts_node_named_child(node, 0), source);
+ }
+
+ /* The first argument's value node of a PHP call (unwrapping `argument`). */
+ static TSNode php_first_arg_value(TSNode call_node) {
+ TSNode none = {0};
+ TSNode args = ts_node_child_by_field_name(call_node, TS_FIELD("arguments"));
+ if (ts_node_is_null(args) || ts_node_named_child_count(args) == 0) {
+ return none;
+ }
+ TSNode arg = ts_node_named_child(args, 0);
+ if (strcmp(ts_node_type(arg), "argument") == 0) {
+ return ts_node_named_child_count(arg) == SKIP_ONE ? ts_node_named_child(arg, 0) : none;
+ }
+ return arg;
+ }
+
+ /* The literal 'prefix' of an attribute-array group (#1146):
+ * Route::group(['prefix' => 'v1/autocomplete', 'as' => ...], function () {...}),
+ * the form Firefly III's whole API is written in. */
+ static const char *php_group_array_prefix(CBMArena *a, TSNode group_call, const char *source) {
+ TSNode attrs = php_first_arg_value(group_call);
+ if (ts_node_is_null(attrs) || strcmp(ts_node_type(attrs), "array_creation_expression") != 0) {
+ return NULL;
+ }
+ uint32_t nc = ts_node_named_child_count(attrs);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode el = ts_node_named_child(attrs, i);
+ if (strcmp(ts_node_type(el), "array_element_initializer") != 0 ||
+ ts_node_named_child_count(el) != PAIR_LEN) {
+ continue;
+ }
+ const char *key = php_plain_string_value(a, ts_node_named_child(el, 0), source);
+ if (key && strcmp(key, "prefix") == 0) {
+ return php_plain_string_value(a, ts_node_named_child(el, SKIP_ONE), source);
+ }
+ }
+ return NULL;
+ }
+
static const char *php_group_prefix_for_call(CBMArena *a, TSNode node, const char *source) {
- const char *parts[PHP_PREFIX_PARTS_MAX];
+ const char *parts[PHP_PREFIX_PARTS_MAX] = {NULL};
int part_count = 0;
TSNode cur = ts_node_parent(node);
for (int depth = 0; depth < PHP_GROUP_WALK_MAX && !ts_node_is_null(cur); depth++) {
@@@ -1855,202 -1873,111 +1915,277 @@@
}
cur = ts_node_parent(cur);
}
- if (part_count == 0) {
+ return route_prefix_compose(a, parts, part_count);
+}
+
+/* #686: compose Go router-group prefixes (Fiber, Gin, Echo: `g := app.Group("/admin")`
+ * then `g.Post("/x")`). Starting at the route call's receiver, follow:
+ * - an identifier to the latest binding before the use (`:=`, `=`, `var`)
+ * among the preceding statements of the enclosing blocks of the same
+ * function (a re-bound variable uses the binding in effect at the call),
+ * - a `recv.Group("<literal>", ...)` call to its literal prefix and receiver,
+ * until the root router (`fiber.New()`, `gin.Default()`, a parameter ...).
+ * A non-literal group prefix returns NULL: the route stays un-prefixed rather
+ * than carrying a guessed path. Groups passed across functions are not
+ * followed (parameters are roots). */
+enum { GO_GROUP_HOPS_MAX = 32 };
+
+static const char *go_string_literal_value(CBMArena *a, TSNode n, const char *source) {
+ const char *k = ts_node_type(n);
+ if (strcmp(k, "interpreted_string_literal") != 0 && strcmp(k, "raw_string_literal") != 0) {
return NULL;
}
- /* parts[] is inner-first; compose outer-first. Ensure exactly one '/'
- * between segments and a leading '/'. */
- char buf[CBM_SZ_256];
- size_t pos = 0;
- for (int i = part_count - 1; i >= 0; i--) {
- const char *seg = parts[i];
- while (*seg == '/') {
- seg++;
+ char *t = cbm_node_text(a, n, source);
+ size_t len = t ? strlen(t) : 0;
+ if (len < 2) {
+ return NULL;
+ }
+ t[len - 1] = '\0';
+ return t + 1;
+}
+
+/* 1 = `recv.Group("<lit>")` (receiver + literal written out), 0 = not a Group
+ * call, -1 = a Group call whose prefix is not a string literal. */
+static int go_group_call(CBMArena *a, TSNode expr, const char *source, TSNode *recv_out,
+ const char **prefix_out) {
+ if (strcmp(ts_node_type(expr), "call_expression") != 0) {
+ return 0;
+ }
+ TSNode fn = ts_node_child_by_field_name(expr, TS_FIELD("function"));
+ if (ts_node_is_null(fn) || strcmp(ts_node_type(fn), "selector_expression") != 0) {
+ return 0;
+ }
+ TSNode field = ts_node_child_by_field_name(fn, TS_FIELD("field"));
+ const char *ft = ts_node_is_null(field) ? NULL : cbm_node_text(a, field, source);
+ if (!ft || strcmp(ft, "Group") != 0) {
+ return 0;
+ }
+ *recv_out = ts_node_child_by_field_name(fn, TS_FIELD("operand"));
+ TSNode args = ts_node_child_by_field_name(expr, TS_FIELD("arguments"));
+ *prefix_out = NULL;
+ if (!ts_node_is_null(args) && ts_node_named_child_count(args) > 0) {
+ *prefix_out = go_string_literal_value(a, ts_node_named_child(args, 0), source);
+ }
+ return *prefix_out ? 1 : -1;
+}
+
+/* Value bound to `name` by one var_spec (`var a, b = x, y`), or a null node. */
+static TSNode go_var_spec_binding(CBMArena *a, TSNode spec, const char *name, const char *source) {
+ TSNode none = {0};
+ TSNode value = ts_node_child_by_field_name(spec, TS_FIELD("value"));
+ if (ts_node_is_null(value)) {
+ return none;
+ }
+ uint32_t idx = 0;
+ uint32_t nc = ts_node_named_child_count(spec);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_named_child(spec, i);
+ if (strcmp(ts_node_type(c), "identifier") != 0) {
+ continue;
}
- size_t sl = strlen(seg);
- if (sl == 0) {
+ const char *t = cbm_node_text(a, c, source);
+ if (t && strcmp(t, name) == 0) {
+ return idx < ts_node_named_child_count(value) ? ts_node_named_child(value, idx) : none;
+ }
+ idx++;
+ }
+ return none;
+}
+
+/* Value bound to `name` by statement `stmt`, or a null node. */
+static TSNode go_stmt_binding(CBMArena *a, TSNode stmt, const char *name, const char *source) {
+ TSNode none = {0};
+ const char *k = ts_node_type(stmt);
+ if (strcmp(k, "var_declaration") == 0 || strcmp(k, "var_spec_list") == 0) {
+ for (uint32_t i = ts_node_named_child_count(stmt); i > 0; i--) {
+ TSNode c = ts_node_named_child(stmt, i - 1);
+ TSNode v = strcmp(ts_node_type(c), "var_spec") == 0
+ ? go_var_spec_binding(a, c, name, source)
+ : go_stmt_binding(a, c, name, source);
+ if (!ts_node_is_null(v)) {
+ return v;
+ }
+ }
+ return none;
+ }
+ if (strcmp(k, "short_var_declaration") != 0 && strcmp(k, "assignment_statement") != 0) {
+ return none;
+ }
+ TSNode left = ts_node_child_by_field_name(stmt, TS_FIELD("left"));
+ TSNode right = ts_node_child_by_field_name(stmt, TS_FIELD("right"));
+ if (ts_node_is_null(left) || ts_node_is_null(right)) {
+ return none;
+ }
+ uint32_t nl = ts_node_named_child_count(left);
+ for (uint32_t i = 0; i < nl; i++) {
+ const char *t = cbm_node_text(a, ts_node_named_child(left, i), source);
+ if (t && strcmp(t, name) == 0) {
+ return i < ts_node_named_child_count(right) ? ts_node_named_child(right, i) : none;
+ }
+ }
+ return none;
+}
+
+static bool go_is_function_boundary(const char *k) {
+ return strcmp(k, "function_declaration") == 0 || strcmp(k, "method_declaration") == 0 ||
+ strcmp(k, "func_literal") == 0 || strcmp(k, "source_file") == 0;
+}
+
+/* Latest binding of `name` among the statements preceding `use` in its
+ * enclosing blocks, innermost block first; a null node when none. */
+static TSNode go_find_binding(CBMArena *a, TSNode use, const char *name, const char *source) {
+ TSNode child = use;
+ TSNode cur = ts_node_parent(use);
+ while (!ts_node_is_null(cur) && !go_is_function_boundary(ts_node_type(cur))) {
+ const char *k = ts_node_type(cur);
+ if (strcmp(k, "block") == 0 || strcmp(k, "statement_list") == 0) {
+ for (TSNode s = ts_node_prev_named_sibling(child); !ts_node_is_null(s);
+ s = ts_node_prev_named_sibling(s)) {
+ TSNode v = go_stmt_binding(a, s, name, source);
+ if (!ts_node_is_null(v)) {
+ return v;
+ }
+ }
+ }
+ child = cur;
+ cur = ts_node_parent(cur);
+ }
+ TSNode none = {0};
+ return none;
+}
+
+static const char *go_group_prefix_for_call(CBMArena *a, TSNode call, const char *source) {
+ TSNode fn = ts_node_child_by_field_name(call, TS_FIELD("function"));
+ if (ts_node_is_null(fn) || strcmp(ts_node_type(fn), "selector_expression") != 0) {
+ return NULL;
+ }
+ const char *parts[GO_GROUP_HOPS_MAX] = {NULL};
+ int part_count = 0;
+ TSNode recv = ts_node_child_by_field_name(fn, TS_FIELD("operand"));
+ for (int hop = 0; !ts_node_is_null(recv); hop++) {
+ if (hop >= GO_GROUP_HOPS_MAX) {
+ return NULL; /* chain too deep to trust: never a partial prefix */
+ }
+ const char *k = ts_node_type(recv);
+ if (strcmp(k, "parenthesized_expression") == 0 && ts_node_named_child_count(recv) > 0) {
+ recv = ts_node_named_child(recv, 0);
continue;
}
- if (pos + sl + 2 >= sizeof(buf)) {
- return NULL; /* oversized — leave path un-prefixed */
+ if (strcmp(k, "identifier") == 0) {
+ const char *name = cbm_node_text(a, recv, source);
+ recv = name ? go_find_binding(a, recv, name, source) : (TSNode){0};
+ continue; /* unbound (parameter, package var): root reached */
}
- buf[pos++] = '/';
- memcpy(buf + pos, seg, sl);
- pos += sl;
- while (pos > 1 && buf[pos - 1] == '/') {
- pos--; /* strip trailing slash per segment */
+ TSNode inner = {0};
+ const char *prefix = NULL;
+ int g = go_group_call(a, recv, source, &inner, &prefix);
+ if (g < 0) {
+ return NULL; /* dynamic prefix: do not guess */
+ }
+ if (g == 0) {
+ break; /* root router constructor or other expression */
}
+ parts[part_count++] = prefix; /* inner-first */
+ recv = inner;
}
- buf[pos] = '\0';
- return pos ? cbm_arena_strndup(a, buf, pos) : NULL;
+ return route_prefix_compose(a, parts, part_count);
+}
+
+/* Routes registered inside router groups must carry the composed path — the
+ * resolve passes only see the flat CBMCall, so the enclosing group chain can
+ * only be read here where the AST still exists. Laravel `prefix()->group()`
+ * closures (#952) and Go `x.Group("/p")` receivers (#686). Returns the route
+ * path to store (unchanged when no group prefix applies). */
+static const char *route_path_with_group_prefix(CBMExtractCtx *ctx, TSNode node,
+ const char *callee_name, const char *path) {
+ if (!path || path[0] != '/' || !callee_name ||
+ cbm_service_pattern_route_method(callee_name) == NULL) {
+ return path;
+ }
+ const char *gp = NULL;
+ if (ctx->language == CBM_LANG_PHP) {
+ gp = php_group_prefix_for_call(ctx->arena, node, ctx->source);
+ } else if (ctx->language == CBM_LANG_GO) {
+ gp = go_group_prefix_for_call(ctx->arena, node, ctx->source);
+ }
+ return (gp && gp[0]) ? route_path_with_prefix(ctx->arena, gp, path) : path;
}
+ /* A Laravel route registrar receiver (#1146): the Route facade — also at the
+ * root of a registrar chain, Route::middleware('auth')->get(...) — or the
+ * $router instance route files are loaded with (RouteServiceProvider, Lumen).
+ * The same literal `Route` scope the #952 callee qualification accepts. */
+ static bool php_is_laravel_registrar(CBMArena *a, TSNode call_node, const char *source) {
+ TSNode cur = call_node;
+ for (int hops = 0; hops < PHP_GROUP_WALK_MAX && !ts_node_is_null(cur); hops++) {
+ const char *kind = ts_node_type(cur);
+ if (strcmp(kind, "scoped_call_expression") == 0) {
+ TSNode scope = ts_node_child_by_field_name(cur, TS_FIELD("scope"));
+ const char *st = ts_node_is_null(scope) ? NULL : cbm_node_text(a, scope, source);
+ return st && strcmp(st, "Route") == 0;
+ }
+ if (strcmp(kind, "variable_name") == 0) {
+ const char *vt = cbm_node_text(a, cur, source);
+ return vt && strcmp(vt, "$router") == 0;
+ }
+ if (strcmp(kind, "member_call_expression") != 0) {
+ return false;
+ }
+ cur = ts_node_child_by_field_name(cur, TS_FIELD("object"));
+ }
+ return false;
+ }
+
+ /* Laravel accepts a route URI with or without its leading slash and trims
+ * '/' from both ends: Route::get('users', ...) serves /users, and '' is the
+ * group root. The route passes only mint a Route for a path that starts with
+ * '/', so give a recognised registration's literal URI its slash here, where
+ * the first argument is still at hand (#1146). Returns NULL — leave the call
+ * alone — for any other call, any non-literal URI, or one already slashed:
+ * `$request->get('name')`, `Cache::get('key')` and a collection's get('k')
+ * keep their argument. */
+ static const char *php_laravel_route_uri(CBMArena *a, TSNode call_node, const char *source) {
+ static const char *const verbs[] = {"get", "post", "put", "patch", "delete", NULL};
+ TSNode mname = ts_node_child_by_field_name(call_node, TS_FIELD("name"));
+ const char *verb = ts_node_is_null(mname) ? NULL : cbm_node_text(a, mname, source);
+ bool is_verb = false;
+ for (int i = 0; verb && verbs[i] && !is_verb; i++) {
+ is_verb = strcmp(verb, verbs[i]) == 0;
+ }
+ if (!is_verb || !php_is_laravel_registrar(a, call_node, source)) {
+ return NULL;
+ }
+ TSNode arg0 = php_first_arg_value(call_node);
+ const char *uri = ts_node_is_null(arg0) ? NULL : php_plain_string_value(a, arg0, source);
+ if (!uri || uri[0] == '/') {
+ return NULL;
+ }
+ size_t len = strlen(uri);
+ while (len > 0 && uri[len - SKIP_ONE] == '/') {
+ len--;
+ }
+ return cbm_arena_sprintf(a, "/%.*s", (int)len, uri);
+ }
+
+ /* Is a Laravel registration's action (its last argument) a string — 'index'
+ * inside a Route::controller() group, 'UserController@show'? Such an action
+ * does not say which class owns the method, so resolving it could only guess a
+ * same-named function anywhere in the repo. */
+ static bool php_route_action_is_string(TSNode call_node) {
+ TSNode args = ts_node_child_by_field_name(call_node, TS_FIELD("arguments"));
+ uint32_t nc = ts_node_is_null(args) ? 0 : ts_node_named_child_count(args);
+ if (nc < PAIR_LEN) {
+ return false;
+ }
+ TSNode action = ts_node_named_child(args, nc - SKIP_ONE);
+ if (strcmp(ts_node_type(action), "argument") == 0 &&
+ ts_node_named_child_count(action) == SKIP_ONE) {
+ action = ts_node_named_child(action, 0);
+ }
+ const char *kind = ts_node_type(action);
+ return strcmp(kind, "string") == 0 || strcmp(kind, "encapsed_string") == 0;
+ }
+
static bool is_nested_verilog_call_wrapper(CBMLanguage lang, TSNode node) {
if (lang != CBM_LANG_VERILOG || strcmp(ts_node_type(node), "subroutine_call") != 0) {
return false;
@@@ -4148,10 -4075,39 +4283,24 @@@ CBMInvocationDescriptor handle_calls(CB
args = swift_call_args(node);
}
if (!ts_node_is_null(args)) {
- call.first_string_arg = route_path_with_group_prefix(
- ctx, node, call.callee_name,
- extract_url_or_topic_arg(ctx, args, call.callee_name));
- if (call.first_string_arg && call.first_string_arg[0] == '/') {
+ call.first_string_arg = extract_url_or_topic_arg(ctx, args, call.callee_name);
+ /* #1146: a Laravel registration's slashless URI ('users', '')
+ * becomes '/users' / '/' so it mints and composes below. */
+ const char *laravel_uri = NULL;
+ if (ctx->language == CBM_LANG_PHP && call.callee_name &&
+ cbm_service_pattern_route_method(call.callee_name) != NULL) {
+ laravel_uri = php_laravel_route_uri(ctx->arena, node, ctx->source);
+ if (laravel_uri) {
+ call.first_string_arg = laravel_uri;
+ }
+ }
- /* #952: routes registered inside Laravel `prefix()->group()`
- * closures must carry the composed path — the resolve passes
- * only see the flat CBMCall, so the enclosing chain can only
- * be read here where the AST still exists. */
- if (ctx->language == CBM_LANG_PHP && call.first_string_arg &&
- call.first_string_arg[0] == '/' && call.callee_name &&
- cbm_service_pattern_route_method(call.callee_name) != NULL) {
- const char *gp = php_group_prefix_for_call(ctx->arena, node, ctx->source);
- if (gp && gp[0]) {
- const char *rel = call.first_string_arg;
- while (*rel == '/') {
- rel++;
- }
- call.first_string_arg =
- rel[0] ? cbm_arena_sprintf(ctx->arena, "%s/%s", gp, rel)
- : cbm_arena_strndup(ctx->arena, gp, strlen(gp));
- }
- }
++ /* Laravel prefix()->group() (#952) and Go x.Group("/p") (#686). */
++ call.first_string_arg = route_path_with_group_prefix(ctx, node, call.callee_name,
++ call.first_string_arg);
+ /* A slashless registration's string action would only be
+ * guessed at (#1146): mint its Route without a handler. */
+ if (call.first_string_arg && call.first_string_arg[0] == '/' &&
+ !(laravel_uri && php_route_action_is_string(node))) {
call.second_arg_name = extract_handler_arg(ctx, args);
}
if (ctx->language == CBM_LANG_OBJECTSCRIPT_UDL ||