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 branch 'fix/go-weak-selector-calls' (with main) into fix/go-method-receiver-qn

Martin Vogel9aeea8eb6f8a
diff --cc internal/cbm/extract_defs.c
index 9d9c98712,77e0bbca0..9bf027d17
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@@ -3037,355 -5125,387 +5125,387 @@@ static const char **extract_signature_p
return result;
}
- static const char **extract_return_types(CBMArena *a, TSNode rt_node, const char *source,
- CBMLanguage lang) {
- (void)lang;
- if (ts_node_is_null(rt_node)) {
- return NULL;
- }
-
- const char *types[MAX_RETURN_TYPES];
- int count = 0;
+ // --- Function definition extraction ---
- if (strcmp(ts_node_type(rt_node), "parameter_list") == 0) {
- extract_go_multi_return(a, rt_node, source, types, &count);
- } else {
- add_cleaned_type(a, types, &count, cbm_node_text(a, rt_node, source));
+ // For C++/CUDA template_declaration, find the inner function_definition or declaration.
+ static TSNode unwrap_template_inner(TSNode node, CBMLanguage lang) {
+ TSNode inner = find_cpp_template_inner_node(node, lang);
+ if (!ts_node_is_null(inner)) {
+ return inner;
}
-
- return build_type_array(a, types, count);
+ return node;
}
- // Extract param_types from a parameter list node.
- // Returns NULL-terminated arena-allocated array.
- // Extract type text from a TypeScript type_annotation child.
- static char *extract_ts_param_type(CBMArena *a, TSNode param, const char *source) {
- TSNode ta = cbm_find_child_by_kind(param, "type_annotation");
- if (ts_node_is_null(ta)) {
- return NULL;
- }
- uint32_t tanc = ts_node_named_child_count(ta);
- for (uint32_t ti = 0; ti < tanc; ti++) {
- TSNode tch = ts_node_named_child(ta, ti);
- if (ts_node_is_null(tch)) {
- continue;
+ // C/C++/CUDA/GLSL: parameters live on function_declarator inside declarator chain.
+ static TSNode find_c_params(TSNode func_node) {
+ TSNode decl = ts_node_child_by_field_name(func_node, TS_FIELD("declarator"));
+ for (int d = 0; d < DECLARATOR_DEPTH_LIMIT && !ts_node_is_null(decl); d++) {
+ TSNode params = ts_node_child_by_field_name(decl, TS_FIELD("parameters"));
+ if (!ts_node_is_null(params)) {
+ return params;
}
- const char *tk = ts_node_type(tch);
- if (strcmp(tk, "type_identifier") == 0 || strcmp(tk, "generic_type") == 0 ||
- strcmp(tk, "predefined_type") == 0) {
- return cbm_node_text(a, tch, source);
+ TSNode nested = ts_node_child_by_field_name(decl, TS_FIELD("declarator"));
+ /* `API RetT *name(...)` misread as `RetT::*name(...)` (MISSING "::"):
+ * the function declarator is on the qualifier's name side. */
+ if (ts_node_is_null(nested) && cbm_c_qualifier_is_recovered(decl)) {
+ nested = ts_node_child_by_field_name(decl, TS_FIELD("name"));
+ }
+ /* tree-sitter-cpp and tree-sitter-cuda do not assign the nested
+ * function declarator a `declarator` field when a reference return
+ * wraps it (`Item& operator[](int)`). That wrapper has one named child;
+ * descend only for this reproduced grammar shape. */
+ if (ts_node_is_null(nested) && strcmp(ts_node_type(decl), "reference_declarator") == 0 &&
+ ts_node_named_child_count(decl) > 0) {
+ nested = ts_node_named_child(decl, 0);
}
+ decl = nested;
}
- return NULL;
- }
-
- // Check if a param node type is a generic parameter-like node.
- static bool is_generic_param_kind(const char *pk) {
- return strcmp(pk, "formal_parameter") == 0 || strcmp(pk, "parameter") == 0 ||
- strcmp(pk, "parameter_declaration") == 0 || strcmp(pk, "spread_parameter") == 0 ||
- strcmp(pk, "simple_parameter") == 0 || strcmp(pk, "variadic_parameter") == 0 ||
- strcmp(pk, "typed_parameter") == 0;
+ TSNode null_node = {0};
+ return null_node;
}
- // Resolve param type for JVM/misc languages (Kotlin, Scala, Dart, Groovy, OCaml).
- static char *resolve_jvm_param_type(CBMArena *a, TSNode param, const char *pk, const char *source,
- CBMLanguage lang) {
- if (strcmp(pk, "parameter") != 0 && strcmp(pk, "formal_parameter") != 0) {
- return NULL;
+ /* Resolve the parameter-list node across the grammars used by the generic
+ * definition extractor. Newer tree-sitter-kotlin exposes
+ * `function_value_parameters` as a direct child rather than a `parameters`
+ * field. Keep the legacy and ordered signature extractors on the same node so
+ * adding positional metadata does not replace or reinterpret `param_types`. */
+ static TSNode find_function_params(TSNode func_node, CBMLanguage lang) {
+ TSNode params = ts_node_child_by_field_name(func_node, TS_FIELD("parameters"));
+ // ObjectScript exposes the parameter list under a `parameter_list` field.
+ if (ts_node_is_null(params) &&
+ (lang == CBM_LANG_OBJECTSCRIPT_UDL || lang == CBM_LANG_OBJECTSCRIPT_ROUTINE)) {
+ params = ts_node_child_by_field_name(func_node, TS_FIELD("parameter_list"));
}
- if (lang == CBM_LANG_KOTLIN) {
- TSNode tn = ts_node_child_by_field_name(param, TS_FIELD("type"));
- if (!ts_node_is_null(tn)) {
- return cbm_node_text(a, tn, source);
- }
- TSNode ut = cbm_find_child_by_kind(param, "user_type");
- return ts_node_is_null(ut) ? NULL : cbm_node_text(a, ut, source);
+ if (ts_node_is_null(params) && lang == CBM_LANG_OBJECTSCRIPT_ROUTINE) {
+ params = cbm_find_child_by_kind(func_node, "parameter_list");
}
- if (lang == CBM_LANG_SCALA || lang == CBM_LANG_DART) {
- TSNode tid = cbm_find_child_by_kind(param, "type_identifier");
- return ts_node_is_null(tid) ? NULL : cbm_node_text(a, tid, source);
+ if (ts_node_is_null(params) && lang == CBM_LANG_OBJECTSCRIPT_UDL) {
+ TSNode method_definition = cbm_find_child_by_kind(func_node, "method_definition");
+ if (!ts_node_is_null(method_definition)) {
+ params = cbm_find_child_by_kind(method_definition, "arguments");
+ }
}
- if (lang == CBM_LANG_GROOVY) {
- TSNode tn = ts_node_child_by_field_name(param, TS_FIELD("type"));
- return ts_node_is_null(tn) ? NULL : cbm_node_text(a, tn, source);
+ if (ts_node_is_null(params) && lang == CBM_LANG_KOTLIN) {
+ params = cbm_find_child_by_kind(func_node, "function_value_parameters");
}
- if (lang == CBM_LANG_OCAML) {
- TSNode tp = cbm_find_child_by_kind(param, "typed_pattern");
- if (!ts_node_is_null(tp)) {
- TSNode tn = ts_node_child_by_field_name(tp, TS_FIELD("type"));
- if (!ts_node_is_null(tn)) {
- return cbm_node_text(a, tn, source);
- }
- }
- return NULL;
+ if (ts_node_is_null(params) && (lang == CBM_LANG_C || lang == CBM_LANG_CPP ||
+ lang == CBM_LANG_CUDA || lang == CBM_LANG_GLSL)) {
+ params = find_c_params(func_node);
}
- return NULL;
+ return params;
}
- // Resolve parameter type text for a single param node.
- static char *resolve_param_type_text(CBMArena *a, TSNode param, const char *source,
- CBMLanguage lang) {
- const char *pk = ts_node_type(param);
-
- if ((lang == CBM_LANG_OBJECTSCRIPT_UDL || lang == CBM_LANG_OBJECTSCRIPT_ROUTINE) &&
- (strcmp(pk, "argument") == 0 || strcmp(pk, "tag_parameter") == 0)) {
- TSNode return_type = cbm_find_child_by_kind(param, "return_type");
- TSNode typename = ts_node_is_null(return_type)
- ? (TSNode){0}
- : cbm_find_child_by_kind(return_type, "typename");
- return ts_node_is_null(typename) ? NULL : cbm_node_text(a, typename, source);
- }
+ /* ── C-family declared return type ──────────────────────────────────
+ * The C-family grammars split a declared return type three ways: the `type`
+ * field (`char`), sibling type_qualifier nodes (`const`), and the
+ * pointer/reference declarators wrapping the function declarator (`*`). Taking
+ * only the `type` field published `const char *get_name(void)` as "char".
+ *
+ * Canonical spelling: leading cv-qualifiers in source order, the base type text
+ * verbatim, then one space and the declarator markers outermost-first with no
+ * space between them — `const char *`, `char **`, `Text &`, `Text *&`. A
+ * qualifier on a pointer level follows its `*` and is separated from the next
+ * marker by a space: `char *const *`. A qualifier written after the base type
+ * (`char const *`) is normalized to the leading position. */
- if (lang == CBM_LANG_TYPESCRIPT || lang == CBM_LANG_TSX || lang == CBM_LANG_ARKTS) {
- if (strcmp(pk, "required_parameter") == 0 || strcmp(pk, "optional_parameter") == 0) {
- return extract_ts_param_type(a, param, source);
- }
- return NULL;
- }
+ /* Output sink: measures when buf is NULL, writes otherwise. Rendering twice
+ * sizes the arena allocation exactly without a second copy of the logic. */
+ typedef struct {
+ char *buf;
+ size_t len;
+ } c_rt_out_t;
- if (lang == CBM_LANG_KOTLIN || lang == CBM_LANG_SCALA || lang == CBM_LANG_DART ||
- lang == CBM_LANG_GROOVY || lang == CBM_LANG_OCAML) {
- return resolve_jvm_param_type(a, param, pk, source, lang);
+ static void c_rt_put(c_rt_out_t *out, const char *text, size_t n) {
+ if (out->buf) {
+ memcpy(out->buf + out->len, text, n);
}
+ out->len += n;
+ }
- // Generic: parameter-like nodes with "type" field
- if (is_generic_param_kind(pk)) {
- TSNode tn = ts_node_child_by_field_name(param, TS_FIELD("type"));
- if (!ts_node_is_null(tn)) {
- return cbm_node_text(a, tn, source);
- }
- }
- return NULL;
+ static void c_rt_put_str(c_rt_out_t *out, const char *text) {
+ c_rt_put(out, text, strlen(text));
}
- // Add a cleaned, deduplicated type to the types array.
- static void add_dedup_type(CBMArena *a, const char **types, int *count, char *type_text) {
- if (!type_text || !type_text[0]) {
- return;
+ static void c_rt_put_node(c_rt_out_t *out, TSNode node, const char *source) {
+ uint32_t start = ts_node_start_byte(node);
+ uint32_t end = ts_node_end_byte(node);
+ if (end > start) {
+ c_rt_put(out, source + start, (size_t)(end - start));
}
- char *cleaned = clean_type_name(a, type_text);
- if (!cleaned || !cleaned[0] || is_builtin_type(cleaned)) {
- return;
+ }
+
+ /* type_qualifier also covers keywords that are not part of the type
+ * (`constexpr`, `_Noreturn`, `mutable`, `__extension__`, …). Keep only the ones
+ * that are, so `constexpr int f()` still returns "int". */
+ static bool is_c_return_cv_qualifier(TSNode node, const char *source) {
+ static const char *const kept[] = {"const", "volatile", "restrict", "__restrict",
+ "__restrict__", "_Atomic", NULL};
+ if (strcmp(ts_node_type(node), "type_qualifier") != 0) {
+ return false;
}
- for (int j = 0; j < *count; j++) {
- if (strcmp(types[j], cleaned) == 0) {
- return;
+ uint32_t start = ts_node_start_byte(node);
+ uint32_t end = ts_node_end_byte(node);
+ size_t len = end > start ? (size_t)(end - start) : 0;
+ for (const char *const *k = kept; *k; k++) {
+ if (strlen(*k) == len && memcmp(source + start, *k, len) == 0) {
+ return true;
}
}
- types[(*count)++] = cleaned;
+ return false;
}
- static const char **extract_param_types(CBMArena *a, TSNode params, const char *source,
- CBMLanguage lang) {
- if (ts_node_is_null(params)) {
- return NULL;
- }
-
- const char *types[MAX_PARAMS];
- int count = 0;
+ static bool is_c_declarator_lang(CBMLanguage lang) {
+ return lang == CBM_LANG_C || lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA ||
+ lang == CBM_LANG_GLSL || lang == CBM_LANG_HLSL || lang == CBM_LANG_ISPC ||
+ lang == CBM_LANG_SLANG || lang == CBM_LANG_OBJC;
+ }
- uint32_t nc = ts_node_child_count(params);
- for (uint32_t i = 0; i < nc && count < MAX_PARAMS_MINUS_1; i++) {
- TSNode param = ts_node_child(params, i);
- if (ts_node_is_null(param) || !ts_node_is_named(param)) {
- continue;
- }
- add_dedup_type(a, types, &count, resolve_param_type_text(a, param, source, lang));
+ /* C-family `struct X` / `union X` / `enum X` / `class X` WITHOUT a body is a
+ * reference to the type — a forward declaration, a variable, parameter or field
+ * type, a sizeof operand, the aliased side of `typedef struct X Y;` — never its
+ * definition. Minting a def for it put a one-line Class/Enum node on the type's
+ * QN: when the definition shares that QN (same file, or a same-stem .h/.c pair,
+ * whose module QNs coincide), the later or smaller-path reference displaced the
+ * definition, and elsewhere it left a phantom type node in every file that
+ * merely mentions the type (`struct timeval` in redis-cli.c). */
+ static bool is_c_tag_reference(CBMLanguage lang, TSNode node, const char *kind) {
+ if (!is_c_declarator_lang(lang)) {
+ return false;
}
-
- if (count == 0) {
- return NULL;
+ if (strcmp(kind, "struct_specifier") != 0 && strcmp(kind, "union_specifier") != 0 &&
+ strcmp(kind, "enum_specifier") != 0 && strcmp(kind, "class_specifier") != 0) {
+ return false;
}
+ return ts_node_is_null(ts_node_child_by_field_name(node, TS_FIELD("body")));
+ }
- const char **result =
- (const char **)cbm_arena_alloc(a, (count + NULL_TERM) * sizeof(const char *));
- for (int i = 0; i < count; i++) {
- result[i] = types[i];
- }
- result[count] = NULL;
- return result;
+ /* Languages whose plain `enum` is UNSCOPED: its enumerators are names of the
+ * scope that holds the enum, not of the enum (C, C++ and its CUDA dialect,
+ * Objective-C). `XXH_OK` of `typedef enum { XXH_OK, XXH_ERROR } XXH_errorcode;`
+ * is written and looked up as plain XXH_OK, so its QN is `<scope>.XXH_OK`. */
+ static bool c_enum_lang(CBMLanguage lang) {
+ return lang == CBM_LANG_C || lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA ||
+ lang == CBM_LANG_OBJC;
}
- /* Internal, occurrence-preserving signature extraction. `param_types` above is
- * the legacy USES_TYPE projection: it intentionally drops builtins and
- * duplicate types. Semantic call resolution needs a different contract — one
- * type entry per source-level call argument, in declaration order. */
- static const char signature_unknown_type[] = "?";
+ /* Is a struct, union or class around an unscoped enum the scope of its
+ * enumerators? In C++ (and CUDA; every .h is parsed as C++) it is: the
+ * enumerator is `Brush::ROUND`. C and Objective-C have no struct scope for
+ * ordinary identifiers: an enum declared inside a struct puts its enumerators
+ * in the scope around the struct and the code names them unqualified, so the
+ * struct is no segment of their QN. */
+ static bool c_enum_scope_is_class(CBMLanguage lang) {
+ return lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA;
+ }
- static char *signature_receiver_name(CBMArena *a, TSNode param, const char *source,
- bool unwrap_wrappers) {
- char *name = resolve_param_name(a, param, source);
- if (name && name[0]) {
- return name;
+ /* C++11 `enum class X` / `enum struct X`: the scoping keyword is an anonymous
+ * token child between `enum` and the body. Its enumerators stay `X::A`. */
+ static bool c_enum_is_scoped(TSNode enum_node) {
+ TSNode body = ts_node_child_by_field_name(enum_node, TS_FIELD("body"));
+ uint32_t stop = ts_node_is_null(body) ? UINT32_MAX : ts_node_start_byte(body);
+ uint32_t nc = ts_node_child_count(enum_node);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_child(enum_node, i);
+ if (ts_node_start_byte(c) >= stop) {
+ break;
+ }
+ if (!ts_node_is_named(c) &&
+ (strcmp(ts_node_type(c), "class") == 0 || strcmp(ts_node_type(c), "struct") == 0)) {
+ return true;
+ }
}
+ return false;
+ }
- TSNode candidate = ts_node_child_by_field_name(param, TS_FIELD("pattern"));
- if (ts_node_is_null(candidate)) {
- candidate = ts_node_child_by_field_name(param, TS_FIELD("name"));
- }
- if (ts_node_is_null(candidate)) {
- uint32_t nc = ts_node_named_child_count(param);
- for (uint32_t i = 0; i < nc; i++) {
- TSNode child = ts_node_named_child(param, i);
- const char *ck = ts_node_type(child);
- if (strcmp(ck, "type_annotation") == 0 || strcmp(ck, "decorator") == 0 ||
- strcmp(ck, "attribute_item") == 0) {
- continue;
- }
- candidate = child;
+ /* Render the canonical return type into `out`; returns how many qualifiers and
+ * markers were added around the base type (0 = the base text alone is already
+ * the whole type). The declarator walk is one strict child chain, so it is
+ * O(depth) with no recursion and needs no depth cap. It stops at the first node
+ * that is neither a pointer nor a reference declarator: for a function returning
+ * a function pointer (`int (*f(void))(int)`) that is the outer
+ * function_declarator, which leaves the base type as it was. That node is
+ * handed back through `rest` when the caller asks for it. */
+ static size_t c_rt_render(c_rt_out_t *out, TSNode func_node, TSNode type_node, TSNode declarator,
+ const char *source, TSNode *rest) {
+ size_t added = 0;
+ uint32_t decl_start = ts_node_start_byte(declarator);
+ uint32_t nc = ts_node_named_child_count(func_node);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode ch = ts_node_named_child(func_node, i);
+ if (ts_node_start_byte(ch) >= decl_start) {
break;
}
+ if (is_c_return_cv_qualifier(ch, source)) {
+ c_rt_put_node(out, ch, source);
+ c_rt_put_str(out, " ");
+ added++;
+ }
}
+ c_rt_put_node(out, type_node, source);
- /* Python typed/splat wrappers and TS pattern wrappers place the actual
- * receiver identifier at the first named leaf. */
- for (int depth = 0; depth < 4 && !ts_node_is_null(candidate); depth++) {
- if (ts_node_named_child_count(candidate) == 0) {
- return cbm_node_text(a, candidate, source);
+ bool need_space = true;
+ TSNode decl = declarator;
+ while (!ts_node_is_null(decl)) {
+ const char *dk = ts_node_type(decl);
+ bool is_ref = strcmp(dk, "reference_declarator") == 0;
+ if (!is_ref && strcmp(dk, "pointer_declarator") != 0) {
+ break;
}
- if (!unwrap_wrappers) {
- return NULL;
+ if (need_space) {
+ c_rt_put_str(out, " ");
+ need_space = false;
}
- TSNode next = ts_node_child_by_field_name(candidate, TS_FIELD("pattern"));
- if (ts_node_is_null(next)) {
- next = ts_node_child_by_field_name(candidate, TS_FIELD("name"));
+ /* A reference_declarator opens with its `&` / `&&` token. */
+ TSNode marker = ts_node_child(decl, 0);
+ if (is_ref && !ts_node_is_null(marker) && !ts_node_is_named(marker)) {
+ c_rt_put_node(out, marker, source);
+ } else {
+ c_rt_put_str(out, is_ref ? "&" : "*");
}
- if (ts_node_is_null(next)) {
- next = ts_node_named_child(candidate, 0);
+ added++;
+ uint32_t dn = ts_node_named_child_count(decl);
+ for (uint32_t i = 0; i < dn; i++) {
+ TSNode q = ts_node_named_child(decl, i);
+ if (is_c_return_cv_qualifier(q, source)) {
+ c_rt_put_node(out, q, source);
+ need_space = true;
+ added++;
+ }
}
- candidate = next;
- }
- return NULL;
- }
-
- static bool is_signature_implicit_receiver(CBMArena *a, TSNode param, const char *source,
- CBMLanguage lang, bool method, bool first_parameter) {
- const char *pk = ts_node_type(param);
-
- /* These grammar nodes describe a receiver supplied by member-call syntax,
- * not an argument at the call site. */
- if (strcmp(pk, "self_parameter") == 0 || strcmp(pk, "receiver_parameter") == 0 ||
- strcmp(pk, "explicit_object_parameter_declaration") == 0) {
- return true;
- }
-
- if (lang == CBM_LANG_TYPESCRIPT || lang == CBM_LANG_TSX || lang == CBM_LANG_ARKTS) {
- char *name = signature_receiver_name(a, param, source, false);
- return name && strcmp(name, "this") == 0;
- }
-
- /* Python instance/class receivers are conventional identifiers rather than
- * distinct AST nodes. Only exclude the leading slot on a method path. */
- if (method && first_parameter && lang == CBM_LANG_PYTHON) {
- char *name = signature_receiver_name(a, param, source, true);
- return name && (strcmp(name, "self") == 0 || strcmp(name, "cls") == 0);
- }
- return false;
- }
-
- static bool is_signature_non_parameter(TSNode param, const char *source, CBMLanguage lang) {
- const char *pk = ts_node_type(param);
- if (strcmp(pk, "comment") == 0 || strcmp(pk, "line_comment") == 0 ||
- strcmp(pk, "block_comment") == 0 || strcmp(pk, "attribute_item") == 0 ||
- strcmp(pk, "inner_attribute_item") == 0 || strcmp(pk, "attribute_specifier") == 0 ||
- strcmp(pk, "attribute_declaration") == 0 || strcmp(pk, "annotation") == 0 ||
- strcmp(pk, "decorator") == 0 || strcmp(pk, "modifiers") == 0 ||
- strcmp(pk, "positional_separator") == 0 || strcmp(pk, "keyword_separator") == 0 ||
- strcmp(pk, "type_parameters") == 0 || strcmp(pk, "type_parameter_declaration") == 0) {
- return true;
+ /* tree-sitter-cpp/-cuda give a reference_declarator's inner declarator no
+ * `declarator` field (see find_c_params); it is the one named child. */
+ TSNode inner = ts_node_child_by_field_name(decl, TS_FIELD("declarator"));
+ if (ts_node_is_null(inner) && is_ref && dn > 0) {
+ inner = ts_node_named_child(decl, 0);
+ }
+ decl = inner;
}
-
- /* In C-family declarations `(void)` means zero parameters. The grammar
- * exposes `void` as a named primitive_type child rather than a parameter. */
- if ((lang == CBM_LANG_C || lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA ||
- lang == CBM_LANG_GLSL) &&
- strcmp(pk, "primitive_type") == 0) {
- uint32_t start = ts_node_start_byte(param);
- uint32_t end = ts_node_end_byte(param);
- return end - start == 4 && strncmp(source + start, "void", 4) == 0;
+ if (rest) {
+ *rest = decl; /* the declarator the markers wrap; null when none is left */
}
- return false;
+ return added;
}
- /* Go permits one declaration node to introduce several positional parameters:
- * `func f(a, b T)`. Count its direct name identifiers; unnamed parameter
- * declarations still represent one position. */
- static int signature_param_multiplicity(TSNode param, CBMLanguage lang) {
- if (lang != CBM_LANG_GO) {
- return 1;
+ /* Declared return type of a C-family function/method node whose `type` field is
+ * `type_node`. Any other language, and any type with nothing around its base
+ * type, gets the base type text exactly as before. */
+ static char *c_declared_return_type(CBMExtractCtx *ctx, TSNode func_node, TSNode type_node) {
+ CBMArena *a = ctx->arena;
+ TSNode declarator = ts_node_child_by_field_name(func_node, TS_FIELD("declarator"));
+ if (!is_c_declarator_lang(ctx->language) || ts_node_is_null(declarator)) {
+ return cbm_node_text(a, type_node, ctx->source);
}
- const char *pk = ts_node_type(param);
- if (strcmp(pk, "parameter_declaration") != 0 &&
- strcmp(pk, "variadic_parameter_declaration") != 0) {
- return 1;
+ c_rt_out_t out = {NULL, 0};
+ if (c_rt_render(&out, func_node, type_node, declarator, ctx->source, NULL) == 0) {
+ return cbm_node_text(a, type_node, ctx->source);
}
-
- int field_names = 0;
- int identifier_names = 0;
- uint32_t nc = ts_node_child_count(param);
- for (uint32_t i = 0; i < nc; i++) {
- TSNode child = ts_node_child(param, i);
- if (ts_node_is_null(child) || !ts_node_is_named(child)) {
- continue;
- }
- const char *field = ts_node_field_name_for_child(param, i);
- if (field && strcmp(field, "name") == 0) {
- field_names++;
- }
- if (strcmp(ts_node_type(child), "identifier") == 0) {
- identifier_names++;
- }
+ out.buf = (char *)cbm_arena_alloc(a, out.len + NULL_TERM);
+ if (!out.buf) {
+ return cbm_node_text(a, type_node, ctx->source);
}
- int names = field_names > 0 ? field_names : identifier_names;
- return names > 0 ? names : 1;
+ out.len = 0;
+ (void)c_rt_render(&out, func_node, type_node, declarator, ctx->source, NULL);
+ out.buf[out.len] = '\0';
+ return out.buf;
}
- /* Resolve the full source spelling where the legacy resolver has no supported
- * case (defaulted C++ parameters, Python annotations, complex TS types, etc.).
- * This deliberately does not clean or deduplicate the text. */
- static char *resolve_signature_param_type_text(CBMArena *a, TSNode param, const char *source,
- CBMLanguage lang) {
- TSNode type_node = ts_node_child_by_field_name(param, TS_FIELD("type"));
- if (!ts_node_is_null(type_node)) {
- /* TypeScript-family grammars may expose the `type_annotation` wrapper
- * (including its colon) through the field. Store the actual type node. */
- if (strcmp(ts_node_type(type_node), "type_annotation") == 0 &&
- ts_node_named_child_count(type_node) > 0) {
- type_node = ts_node_named_child(type_node, 0);
- }
- char *type_text = cbm_node_text(a, type_node, source);
- if (type_text && type_text[0]) {
- return type_text;
- }
+ // C++: resolve trailing return type (auto f() -> Type) on a declarator node.
+ // Updates def->return_type and def->return_types if trailing type found.
+ static void resolve_cpp_trailing_return(CBMArena *a, TSNode func_node, const char *source,
+ CBMDefinition *def) {
+ TSNode declarator = ts_node_child_by_field_name(func_node, TS_FIELD("declarator"));
+ if (ts_node_is_null(declarator)) {
+ return;
}
-
- TSNode annotation = cbm_find_child_by_kind(param, "type_annotation");
- if (!ts_node_is_null(annotation)) {
- uint32_t nc = ts_node_named_child_count(annotation);
- if (nc > 0) {
- char *type_text = cbm_node_text(a, ts_node_named_child(annotation, 0), source);
- if (type_text && type_text[0]) {
- return type_text;
+ uint32_t nc = ts_node_named_child_count(declarator);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode ch = ts_node_named_child(declarator, i);
+ if (strcmp(ts_node_type(ch), "trailing_return_type") == 0) {
+ TSNode type_desc = ts_node_named_child_count(ch) > 0 ? ts_node_named_child(ch, 0) : ch;
+ def->return_type = cbm_node_text(a, type_desc, source);
+ if (def->return_type && def->return_type[0]) {
+ const char **rt =
+ (const char **)cbm_arena_alloc(a, RT_PAIR_SIZE * sizeof(const char *));
+ if (rt) {
+ rt[0] = def->return_type;
+ rt[SKIP_CHAR] = NULL;
+ def->return_types = rt;
+ }
}
+ break;
}
}
+ }
- TSNode typed_pattern = cbm_find_child_by_kind(param, "typed_pattern");
- if (!ts_node_is_null(typed_pattern)) {
- type_node = ts_node_child_by_field_name(typed_pattern, TS_FIELD("type"));
- if (!ts_node_is_null(type_node)) {
- char *type_text = cbm_node_text(a, type_node, source);
- if (type_text && type_text[0]) {
- return type_text;
+ /* Compute and store the structural complexity metrics for a definition. */
+ static void set_def_complexity(CBMDefinition *def, TSNode body, const CBMLangSpec *spec) {
+ cbm_complexity_t cx;
+ cbm_compute_complexity(body, spec->branching_node_types, &cx);
+ def->complexity = cx.cyclomatic;
+ def->cognitive = cx.cognitive;
+ def->loop_count = cx.loop_count;
+ def->loop_depth = cx.loop_depth;
+ def->max_access_depth = cx.max_access_depth;
+ }
+
+ /* Extract the bare type name from a Go method receiver node.
+ * The receiver is a parameter_list, e.g. "(s *OrderService)" or "(s Order)".
+ * Walks to the parameter_declaration's `type` field, unwrapping pointer_type
+ * and generic_type, and returns the type_identifier text (e.g. "OrderService").
+ * Returns NULL if no type_identifier is found. */
-static char *go_receiver_type_name(CBMArena *a, TSNode recv, const char *source) {
++char *cbm_go_receiver_type_name(CBMArena *a, TSNode recv, const char *source) {
+ uint32_t nc = ts_node_child_count(recv);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode child = ts_node_child(recv, i);
+ if (strcmp(ts_node_type(child), "parameter_declaration") != 0) {
+ continue;
+ }
+ TSNode tn = ts_node_child_by_field_name(child, TS_FIELD("type"));
+ if (ts_node_is_null(tn)) {
+ continue;
+ }
+ /* Unwrap pointer_type / generic_type down to the type_identifier. */
+ for (int guard = 0; guard < 4 && !ts_node_is_null(tn); guard++) {
+ const char *tk = ts_node_type(tn);
+ if (strcmp(tk, "type_identifier") == 0) {
+ return cbm_node_text(a, tn, source);
+ }
+ if (strcmp(tk, "pointer_type") == 0 || strcmp(tk, "generic_type") == 0) {
+ /* pointer_type: child is the pointee type; generic_type has a
+ * `type` field for the base type_identifier. */
+ TSNode inner = ts_node_child_by_field_name(tn, TS_FIELD("type"));
+ if (ts_node_is_null(inner)) {
+ inner = ts_node_named_child(tn, 0);
+ }
+ tn = inner;
+ continue;
}
+ break;
}
}
- return resolve_param_type_text(a, param, source, lang);
+ return NULL;
}
- static const char **extract_signature_param_types(CBMArena *a, TSNode params, const char *source,
- CBMLanguage lang, bool method, int *out_count) {
- *out_count = 0;
+ /* C++/CUDA: true when name is a GoogleTest test-definition macro whose
+ * invocations parse as function definitions with bare-identifier parameters.
+ * Multiple such macros in one file all share the extracted name (e.g. "TEST"),
+ * causing qualified-name collisions and node loss (#1266). */
+ static bool is_cpp_test_macro(const char *name) {
+ return strcmp(name, "TEST") == 0 || strcmp(name, "TEST_F") == 0 ||
+ strcmp(name, "TEST_P") == 0 || strcmp(name, "TYPED_TEST") == 0 ||
+ strcmp(name, "TYPED_TEST_P") == 0;
+ }
+
+ /* Compose a unique name from a GoogleTest-style macro invocation by appending
+ * the macro arguments: TEST(Suite, Case) -> "TEST_Suite_Case".
+ * Returns an arena-allocated string, or NULL when the parameters cannot be
+ * resolved (caller keeps the original name in that case). */
+ static char *resolve_cpp_test_macro_name(CBMArena *a, const char *macro, TSNode node,
+ const char *source) {
+ TSNode params = ts_node_child_by_field_name(node, TS_FIELD("parameters"));
+ if (ts_node_is_null(params)) {
+ params = find_c_params(node);
+ }
if (ts_node_is_null(params)) {
return NULL;
}
diff --cc tests/test_pipeline.c
index 34af3d989,d413aa9bb..04925d6dc
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@@ -5858,64 -8650,11 +8650,62 @@@ TEST(pipeline_html_embedded_member_call
PASS();
}
+TEST(pipeline_go_method_caller_keeps_lsp_join) {
+ /* #1909: receiver-qualifying method QNs moves the def/textual-call QN to
+ * package.Type.method — go_lsp's enclosing-function QN must move with it
+ * (the ONE-formula contract), or every LSP resolution sourced from inside
+ * a method body loses its caller join and the edge dies. The callee name
+ * collides across receivers so no short-name fallback can mask the loss. */
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_mcall_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_temp_file(tmp, "go.mod", "module example.com/fxmcall\n\ngo 1.22\n");
+ write_temp_file(tmp, "svc/repo.go",
+ "package svc\n"
+ "\n"
+ "type Repo struct {\n"
+ "\tn int\n"
+ "}\n"
+ "\n"
+ "func (r *Repo) Install() int {\n"
+ "\treturn r.helper()\n"
+ "}\n");
+ write_temp_file(tmp, "svc/helper.go",
+ "package svc\n"
+ "\n"
+ "func (r *Repo) helper() int {\n"
+ "\treturn r.n\n"
+ "}\n");
+ write_temp_file(tmp, "other/other.go",
+ "package other\n"
+ "\n"
+ "type Decoy struct{}\n"
+ "\n"
+ "func (d *Decoy) helper() int { return 2 }\n");
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/go_mcall.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ const char *project = cbm_pipeline_project_name(p);
+
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+ ASSERT_TRUE(cross_file_call_exists(s, project, "Install", "helper"));
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmp);
+ PASS();
+}
+
- TEST(pipeline_go_receiver_suppresses_weak_method_edge) {
- char tmp[256];
- snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_recv_XXXXXX");
- if (!cbm_mkdtemp(tmp)) {
- FAIL("tmpdir");
- }
-
+ /* Fixture for the #1906 Go selector-guard probes (sequential and parallel
+ * twins). pad_files > 0 adds filler Go files to push the index over
+ * MIN_FILES_FOR_PARALLEL, because the guard is wired into both resolvers
+ * (pass_calls.c and pass_parallel.c) and each needs its own probe. */
+ static void write_go_receiver_fixture(const char *tmp, int pad_files) {
/* go.mod makes project imports resolvable — real Go repos always have one,
* and import reachability (the unique_name penalty) depends on it. */
write_temp_file(tmp, "go.mod", "module example.com/myapp\n\ngo 1.22\n");
@@@ -15487,13 -23769,22 +23820,23 @@@ SUITE(pipeline)
RUN_TEST(pipeline_python_receiver_suppresses_weak_method_edge);
RUN_TEST(pipeline_python_receiver_keeps_specific_unique_name_member_call);
RUN_TEST(pipeline_html_embedded_member_call_stays_unbound);
+ RUN_TEST(pipeline_go_method_caller_keeps_lsp_join);
RUN_TEST(pipeline_go_receiver_suppresses_weak_method_edge);
+ RUN_TEST(pipeline_go_receiver_parallel_suppresses_weak_method_edge);
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c);
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c_parallel);
+ RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method);
+ RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method_parallel);
RUN_TEST(pipeline_go_bare_ref_never_binds_field);
RUN_TEST(pipeline_go_bare_ref_never_binds_field_parallel);
+ RUN_TEST(pipeline_c_call_reaches_header_function_beside_field);
+ RUN_TEST(pipeline_c_call_reaches_header_function_beside_field_parallel);
+ RUN_TEST(pipeline_c_member_access_binds_field_by_object_type);
+ RUN_TEST(pipeline_c_member_access_binds_field_by_object_type_parallel);
RUN_TEST(pipeline_tsjs_receiver_parallel_keeps_service_edges);
+ RUN_TEST(pipeline_capitalized_axios_import_is_http_client);
+ RUN_TEST(pipeline_capitalized_axios_import_is_http_client_sequential);
+ RUN_TEST(pipeline_python_websocket_decorators_register_routes);
RUN_TEST(pipeline_python_receiver_parallel_suppresses_weak_method_edges);
RUN_TEST(pipeline_python_bare_local_binding_suppresses_weak_edge);
RUN_TEST(pipeline_python_bare_local_binding_parallel_suppresses_weak_edge);