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.

Extract DECORATES for C#/PHP8/Scala/TS attributes + TS type-position USAGE

Martin Vogel3c4e7b4eac25
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index b4112db5f..bbefc6070 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -1211,17 +1211,63 @@ static int count_modifier_annotations(TSNode modifiers, const CBMLangSpec *spec)
return count;
}
-// Find Java/Kotlin/C# modifiers node with annotations.
+// Find the wrapper child that holds annotations/attributes for languages where
+// they are nested under an intermediate node rather than being a prev-sibling:
+// Java/Kotlin/C#/Swift → `modifiers` (contains annotation/attribute)
+// PHP 8 → `attribute_list` (contains attribute_group)
+// Returns a null node when the language has no such wrapper.
static TSNode find_jvm_modifiers(TSNode node, CBMLanguage lang) {
TSNode null_node = {0};
- if (lang != CBM_LANG_JAVA && lang != CBM_LANG_KOTLIN && lang != CBM_LANG_CSHARP) {
+ const char *wrapper = NULL;
+ switch (lang) {
+ case CBM_LANG_JAVA:
+ case CBM_LANG_KOTLIN:
+ case CBM_LANG_SWIFT:
+ wrapper = "modifiers";
+ break;
+ case CBM_LANG_CSHARP:
+ case CBM_LANG_PHP:
+ /* C# attributes live in an `attribute_list` child (modifiers like
+ * `public` are separate `modifier` nodes); PHP 8 likewise nests
+ * `attribute_group` under `attribute_list`. */
+ wrapper = "attribute_list";
+ break;
+ default:
return null_node;
}
- TSNode modifiers = ts_node_child_by_field_name(node, TS_FIELD("modifiers"));
- if (ts_node_is_null(modifiers)) {
- modifiers = cbm_find_child_by_kind(node, "modifiers");
+ TSNode w = ts_node_child_by_field_name(node, wrapper, (uint32_t)strlen(wrapper));
+ if (ts_node_is_null(w)) {
+ w = cbm_find_child_by_kind(node, wrapper);
}
- return modifiers;
+ return w;
+}
+
+// Count direct children of `node` that are decorator/annotation nodes (used by
+// languages like Scala where the annotation is a direct child of the def node).
+static int count_child_decorators(TSNode node, const CBMLangSpec *spec) {
+ int count = 0;
+ uint32_t cc = ts_node_child_count(node);
+ for (uint32_t ci = 0; ci < cc; ci++) {
+ TSNode child = ts_node_child(node, ci);
+ if (cbm_kind_in_set(child, spec->decorator_node_types)) {
+ count++;
+ }
+ }
+ return count;
+}
+
+// Collect direct-child decorator texts into result[] starting at idx.
+static int collect_child_decorators(CBMArena *a, TSNode node, const char *source,
+ const CBMLangSpec *spec, const char **result, int idx,
+ int max) {
+ uint32_t cc = ts_node_child_count(node);
+ for (uint32_t ci = 0; ci < cc && idx < max; ci++) {
+ TSNode child = ts_node_child(node, ci);
+ if (cbm_kind_in_set(child, spec->decorator_node_types)) {
+ result[idx++] = cbm_node_text(a, child, source);
+ }
+ }
+ return idx;
}
// Collect decorator texts from a modifiers node into result array starting at idx.
@@ -1249,7 +1295,10 @@ static const char **extract_decorators(CBMArena *a, TSNode node, const char *sou
while (!ts_node_is_null(prev)) {
if (cbm_kind_in_set(prev, spec->decorator_node_types)) {
count++;
- } else {
+ } else if (ts_node_is_named(prev)) {
+ /* A real preceding construct ends the decorator run. Anonymous
+ * tokens (e.g. TS `export` between `@Decorator` and the
+ * `class_declaration`) are skipped so the decorator is still seen. */
break;
}
prev = ts_node_prev_sibling(prev);
@@ -1257,14 +1306,20 @@ static const char **extract_decorators(CBMArena *a, TSNode node, const char *sou
TSNode modifiers = {0};
int mod_count = 0;
+ int child_count = 0;
if (count == 0) {
modifiers = find_jvm_modifiers(node, lang);
if (!ts_node_is_null(modifiers)) {
mod_count = count_modifier_annotations(modifiers, spec);
}
+ /* Languages like Scala attach the annotation directly as a child of the
+ * definition node (no wrapper, no prev-sibling). */
+ if (mod_count == 0) {
+ child_count = count_child_decorators(node, spec);
+ }
}
- int total = count + mod_count;
+ int total = count + mod_count + child_count;
if (total == 0) {
return NULL;
}
@@ -1280,7 +1335,7 @@ static const char **extract_decorators(CBMArena *a, TSNode node, const char *sou
while (!ts_node_is_null(prev) && idx < count) {
if (cbm_kind_in_set(prev, spec->decorator_node_types)) {
result[idx++] = cbm_node_text(a, prev, source);
- } else {
+ } else if (ts_node_is_named(prev)) {
break;
}
prev = ts_node_prev_sibling(prev);
@@ -1288,6 +1343,9 @@ static const char **extract_decorators(CBMArena *a, TSNode node, const char *sou
if (!ts_node_is_null(modifiers)) {
idx = collect_modifier_decorators(a, modifiers, source, spec, result, idx, total);
}
+ if (child_count > 0) {
+ idx = collect_child_decorators(a, node, source, spec, result, idx, total);
+ }
result[idx] = NULL;
return result;
}
diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c
index aa2cd9a04..0b9c1d46e 100644
--- a/internal/cbm/extract_unified.c
+++ b/internal/cbm/extract_unified.c
@@ -672,6 +672,31 @@ static void scan_infra_bindings(CBMExtractCtx *ctx, TSNode node) {
}
}
+// JS/TS `export_statement` appears in import_node_types so re-exports
+// (`export { X } from './m'`) are treated as an import boundary. But it also
+// wraps exported *declarations* (`export function f(cfg: Config) {}`), and
+// treating those as an import boundary wrongly suppresses USAGE edges for type
+// references inside the exported declaration's signature. Return true when the
+// node is an export that contains a declaration child (i.e. NOT a bare re-export),
+// so the caller skips the import-scope push for it.
+static bool is_export_of_declaration(TSNode node) {
+ if (strcmp(ts_node_type(node), "export_statement") != 0) {
+ return false;
+ }
+ uint32_t n = ts_node_child_count(node);
+ for (uint32_t i = 0; i < n; i++) {
+ const char *ck = ts_node_type(ts_node_child(node, i));
+ if (strcmp(ck, "function_declaration") == 0 || strcmp(ck, "class_declaration") == 0 ||
+ strcmp(ck, "lexical_declaration") == 0 || strcmp(ck, "abstract_class_declaration") == 0 ||
+ strcmp(ck, "interface_declaration") == 0 || strcmp(ck, "enum_declaration") == 0 ||
+ strcmp(ck, "type_alias_declaration") == 0 || strcmp(ck, "variable_declaration") == 0 ||
+ strcmp(ck, "generator_function_declaration") == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
// Push scope markers for function, class, call, and import boundary nodes.
static void push_boundary_scopes(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec,
WalkState *state, uint32_t depth) {
@@ -700,7 +725,8 @@ static void push_boundary_scopes(CBMExtractCtx *ctx, TSNode node, const CBMLangS
if (spec->call_node_types && cbm_kind_in_set(node, spec->call_node_types)) {
push_scope(state, SCOPE_CALL, depth, NULL);
}
- if (spec->import_node_types && cbm_kind_in_set(node, spec->import_node_types)) {
+ if (spec->import_node_types && cbm_kind_in_set(node, spec->import_node_types) &&
+ !is_export_of_declaration(node)) {
push_scope(state, SCOPE_IMPORT, depth, NULL);
}
/* Loop / branch nesting for bottleneck metrics. Loops are gated on named
diff --git a/internal/cbm/lang_specs.c b/internal/cbm/lang_specs.c
index 2524c1aea..e69c879b9 100644
--- a/internal/cbm/lang_specs.c
+++ b/internal/cbm/lang_specs.c
@@ -425,6 +425,7 @@ static const char *scala_var_types[] = {"val_definition", "var_definition", "val
"var_declaration", NULL};
static const char *scala_assign_types[] = {"assignment_expression", NULL};
static const char *scala_throw_types[] = {"throw_expression", NULL};
+static const char *scala_decorator_types[] = {"annotation", NULL};
// ==================== KOTLIN ====================
static const char *kotlin_func_types[] = {"function_declaration", "secondary_constructor",
@@ -1657,7 +1658,7 @@ static const CBMLangSpec lang_specs[CBM_LANG_COUNT] = {
[CBM_LANG_SCALA] = {CBM_LANG_SCALA, scala_func_types, scala_class_types, empty_types,
scala_module_types, scala_call_types, scala_import_types,
scala_import_types, scala_branch_types, scala_var_types, scala_assign_types,
- scala_throw_types, NULL, empty_types, scala_env_funcs, NULL,
+ scala_throw_types, NULL, scala_decorator_types, scala_env_funcs, NULL,
tree_sitter_scala, NULL},
// CBM_LANG_KOTLIN
diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.c
index a13bbf497..78f9581dc 100644
--- a/internal/cbm/lsp/py_lsp.c
+++ b/internal/cbm/lsp/py_lsp.c
@@ -1919,6 +1919,37 @@ static void py_walk_if_statement(PyLSPContext* ctx, TSNode if_node) {
}
}
+/* Map a Python infix operator token to its dunder method name. */
+static const char* py_binop_dunder(const char* op_text) {
+ if (!op_text) return NULL;
+ if (strcmp(op_text, "+") == 0) return "__add__";
+ if (strcmp(op_text, "-") == 0) return "__sub__";
+ if (strcmp(op_text, "*") == 0) return "__mul__";
+ if (strcmp(op_text, "/") == 0) return "__truediv__";
+ if (strcmp(op_text, "//") == 0) return "__floordiv__";
+ if (strcmp(op_text, "%") == 0) return "__mod__";
+ if (strcmp(op_text, "**") == 0) return "__pow__";
+ if (strcmp(op_text, "<<") == 0) return "__lshift__";
+ if (strcmp(op_text, ">>") == 0) return "__rshift__";
+ if (strcmp(op_text, "&") == 0) return "__and__";
+ if (strcmp(op_text, "|") == 0) return "__or__";
+ if (strcmp(op_text, "^") == 0) return "__xor__";
+ if (strcmp(op_text, "@") == 0) return "__matmul__";
+ return NULL;
+}
+
+/* If `recv` is a user-defined NAMED type that defines `dunder`, emit a CALLS
+ * edge to that dunder method (operator-overload / subscript desugaring). This
+ * models `a + b` → T.__add__ and `s[k]` → T.__getitem__ as calls. */
+static void py_emit_dunder_call(PyLSPContext* ctx, const CBMType* recv, const char* dunder) {
+ if (!recv || recv->kind != CBM_TYPE_NAMED || !dunder) return;
+ const CBMRegisteredFunc* f =
+ py_lookup_attribute(ctx, recv->data.named.qualified_name, dunder);
+ if (f && f->qualified_name) {
+ py_emit_resolved_call(ctx, f->qualified_name, "lsp_operator_dunder", 0.85f);
+ }
+}
+
static void py_resolve_calls_in(PyLSPContext* ctx, TSNode node) {
if (!ctx || ts_node_is_null(node)) return;
const char* k = ts_node_type(node);
@@ -1931,6 +1962,26 @@ static void py_resolve_calls_in(PyLSPContext* ctx, TSNode node) {
py_emit_call_for(ctx, node);
}
+ // Operator-overload desugaring: `a + b` calls type(a).__add__,
+ // `s[k]` calls type(s).__getitem__. Only emit when the receiver is a
+ // user-defined type that actually declares the dunder (built-ins resolve
+ // to typeshed and would create noisy edges).
+ if (strcmp(k, "binary_operator") == 0) {
+ TSNode left = ts_node_child_by_field_name(node, "left", 4);
+ TSNode op = ts_node_child_by_field_name(node, "operator", 8);
+ if (!ts_node_is_null(left) && !ts_node_is_null(op)) {
+ const char* dunder = py_binop_dunder(py_node_text(ctx, op));
+ if (dunder) {
+ py_emit_dunder_call(ctx, py_eval_expr_type(ctx, left), dunder);
+ }
+ }
+ } else if (strcmp(k, "subscript") == 0) {
+ TSNode value = ts_node_child_by_field_name(node, "value", 5);
+ if (!ts_node_is_null(value)) {
+ py_emit_dunder_call(ctx, py_eval_expr_type(ctx, value), "__getitem__");
+ }
+ }
+
// if_statement gets special-case narrowing.
if (strcmp(k, "if_statement") == 0) {
py_walk_if_statement(ctx, node);
diff --git a/src/pipeline/pass_semantic.c b/src/pipeline/pass_semantic.c
index 59083bb00..8ca4f5062 100644
--- a/src/pipeline/pass_semantic.c
+++ b/src/pipeline/pass_semantic.c
@@ -178,19 +178,35 @@ static const char *resolve_as_class(const cbm_registry_t *reg, const char *name,
return res.qualified_name;
}
-/* Extract decorator function name: "@app.route('/api')" → "app.route" */
+/* Extract decorator function name from the raw attribute/annotation text.
+ * Handles the per-language surface syntaxes:
+ * Python/TS: "@app.route('/api')" → "app.route"
+ * Java/Kotlin:"@Override" → "Override"
+ * C#: "[Log]" / "[Log(...)]" → "Log"
+ * Rust: "#[derive(Debug)]" → "derive"
+ * "#[allow(dead_code)]" → "allow"
+ * PHP 8: "#[Route('/users')]" → "Route"
+ * Swift: "@discardableResult" → "discardableResult"
+ * Scala: "@deprecated(...)" → "deprecated"
+ * Returns the leading identifier path, stopping at the first '(' or '['
+ * argument list and trimming any wrapping bracket/`@`/`#` punctuation. */
static void extract_decorator_func(const char *dec, char *out, size_t outsz) {
out[0] = '\0';
if (!dec) {
return;
}
const char *start = dec;
- if (*start == '@') {
+ /* Strip leading sigils: '@' (Py/TS/JVM/Swift/Scala), '#' + '[' (Rust/PHP8),
+ * or a bare '[' (C# attribute). */
+ while (*start == '@' || *start == '#' || *start == '[' || *start == ' ') {
start++;
}
- /* Find opening paren */
- const char *paren = strchr(start, '(');
- size_t len = paren ? (size_t)(paren - start) : strlen(start);
+ /* The name ends at the first argument list ('(' or '[') or wrapper ']'. */
+ size_t len = 0;
+ while (start[len] && start[len] != '(' && start[len] != '[' && start[len] != ']' &&
+ start[len] != ' ' && start[len] != ',') {
+ len++;
+ }
if (len == 0 || len >= outsz) {
return;
}
@@ -339,6 +355,18 @@ static void resolve_decorator(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *no
}
cbm_resolution_t res =
cbm_registry_resolve(ctx->registry, func_name, module_qn, imp_keys, imp_vals, imp_count);
+ if ((!res.qualified_name || res.qualified_name[0] == '\0') &&
+ !strchr(func_name, '.')) {
+ /* C# attributes are referenced by their short name (`[Log]`) but declared
+ * with the conventional `Attribute` suffix (`class LogAttribute`). Retry
+ * with the suffix appended so the declaration resolves. */
+ char with_suffix[CBM_SZ_256];
+ int wn = snprintf(with_suffix, sizeof(with_suffix), "%sAttribute", func_name);
+ if (wn > 0 && (size_t)wn < sizeof(with_suffix)) {
+ res = cbm_registry_resolve(ctx->registry, with_suffix, module_qn, imp_keys, imp_vals,
+ imp_count);
+ }
+ }
if (!res.qualified_name || res.qualified_name[0] == '\0') {
return;
}