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.

Add gRPC stub detection in call resolution and chained call extraction

teste7893a2390a2
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.c
index 24b59c4a4..2bd69d004 100644
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@ -101,18 +101,65 @@ static bool lean_is_in_type_position(TSNode node) {
return false;
}
+/* Resolve a selector_expression that may chain through call_expressions.
+ * Go pattern: pb.NewFooClient(conn).GetBar → "pb.NewFooClient.GetBar"
+ * Without this, cbm_node_text returns full text including args/parens.
+ * Iteratively walks the chain: selector → operand(call) → function(selector) → ... */
+static char *resolve_chained_selector(CBMArena *a, TSNode sel, const char *source) {
+ TSNode operand = ts_node_child_by_field_name(sel, TS_FIELD("operand"));
+ TSNode field = ts_node_child_by_field_name(sel, TS_FIELD("field"));
+ if (ts_node_is_null(operand) || ts_node_is_null(field) ||
+ strcmp(ts_node_type(operand), "call_expression") != 0) {
+ return cbm_node_text(a, sel, source);
+ }
+
+ /* Operand is a call_expression — extract its callee iteratively.
+ * Walk: call_expression → function field → if selector_expression, repeat. */
+ char *method = cbm_node_text(a, field, source);
+ TSNode inner = operand;
+ enum { MAX_CHAIN_DEPTH = 4 };
+ for (int depth = 0; depth < MAX_CHAIN_DEPTH; depth++) {
+ TSNode fn = ts_node_child_by_field_name(inner, TS_FIELD("function"));
+ if (ts_node_is_null(fn)) {
+ break;
+ }
+ const char *fnk = ts_node_type(fn);
+ if (strcmp(fnk, "selector_expression") == 0) {
+ /* Check if this selector also chains through a call */
+ TSNode inner_op = ts_node_child_by_field_name(fn, TS_FIELD("operand"));
+ if (!ts_node_is_null(inner_op) &&
+ strcmp(ts_node_type(inner_op), "call_expression") == 0) {
+ inner = inner_op;
+ continue;
+ }
+ }
+ /* Reached a non-chained callee — extract its text */
+ char *base = cbm_node_text(a, fn, source);
+ if (base && method) {
+ return cbm_arena_sprintf(a, "%s.%s", base, method);
+ }
+ return method;
+ }
+
+ /* Fallback: just return the method name */
+ return method;
+}
+
// Try common field-based callee resolution (function, name, method fields).
static char *extract_callee_from_fields(CBMArena *a, TSNode node, const char *source) {
// Try "function" field
TSNode func_node = ts_node_child_by_field_name(node, TS_FIELD("function"));
if (!ts_node_is_null(func_node)) {
const char *fk = ts_node_type(func_node);
+ if (strcmp(fk, "selector_expression") == 0) {
+ return resolve_chained_selector(a, func_node, source);
+ }
if (strcmp(fk, "identifier") == 0 || strcmp(fk, "simple_identifier") == 0 ||
- strcmp(fk, "selector_expression") == 0 || strcmp(fk, "attribute") == 0 ||
- strcmp(fk, "member_expression") == 0 || strcmp(fk, "field_expression") == 0 ||
- strcmp(fk, "dot") == 0 || strcmp(fk, "function") == 0 ||
- strcmp(fk, "dotted_identifier") == 0 || strcmp(fk, "member_access_expression") == 0 ||
- strcmp(fk, "scoped_identifier") == 0 || strcmp(fk, "qualified_identifier") == 0) {
+ strcmp(fk, "attribute") == 0 || strcmp(fk, "member_expression") == 0 ||
+ strcmp(fk, "field_expression") == 0 || strcmp(fk, "dot") == 0 ||
+ strcmp(fk, "function") == 0 || strcmp(fk, "dotted_identifier") == 0 ||
+ strcmp(fk, "member_access_expression") == 0 || strcmp(fk, "scoped_identifier") == 0 ||
+ strcmp(fk, "qualified_identifier") == 0) {
return cbm_node_text(a, func_node, source);
}
}
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index 7ef83e165..9eb24a75c 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -1217,9 +1217,18 @@ static void emit_grpc_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source, cons
const cbm_resolution_t *res) {
char service[CBM_SZ_256];
char method[CBM_SZ_256];
+ /* Try callee_name first (e.g., "pb.NewCartServiceClient.GetCart") */
if (!extract_grpc_service_method(call->callee_name, service, sizeof(service), method,
sizeof(method))) {
- return;
+ /* Fallback: try the resolved QN for Go chained calls.
+ * Go pattern: pb.NewCartServiceClient(conn).GetCart(ctx, req)
+ * callee_name = "GetCart", QN = "...CartServiceClient.GetCart"
+ * The QN contains the full ServiceClient.Method pattern. */
+ if (!res->qualified_name ||
+ !extract_grpc_service_method(res->qualified_name, service, sizeof(service), method,
+ sizeof(method))) {
+ return;
+ }
}
char route_qn[CBM_SZ_512];
@@ -1326,6 +1335,18 @@ static void emit_service_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
svc = CBM_SVC_ROUTE_REG;
}
+ /* Detect gRPC stub method calls by resolved QN.
+ * Go pattern: pb.NewCartServiceClient(conn).GetCart(ctx, req)
+ * Tree-sitter extracts GetCart as the callee, which resolves to the
+ * generated pb interface method (QN contains "ServiceClient"). */
+ if (svc == CBM_SVC_NONE && res->qualified_name) {
+ if (strstr(res->qualified_name, "ServiceClient") != NULL ||
+ strstr(res->qualified_name, "ServiceGrpc") != NULL ||
+ strstr(res->qualified_name, "Servicer") != NULL) {
+ svc = CBM_SVC_GRPC;
+ }
+ }
+
if (svc == CBM_SVC_ROUTE_REG) {
const char *handler_ref = NULL;
const char *route_path = find_route_path_in_args(call, &handler_ref);