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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(grammar): extract calls for 11 niche grammars (HDL/asm/build/proof langs)
Martin Vogelda506f7b179b
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.cindex 369453898..64433d47e 100644--- a/internal/cbm/extract_calls.c+++ b/internal/cbm/extract_calls.c@@ -783,6 +783,173 @@ static char *extract_meson_callee(CBMArena *a, TSNode node, const char *source,return ts_node_is_null(cmd) ? NULL : cbm_node_text(a, cmd, source);}+// Descend left-most through wrapper nodes to the first identifier-bearing leaf.+// Used by HDL call nodes whose callee identifier is nested under one or more+// grammar wrappers (Verilog tf_call -> simple_identifier; SystemVerilog+// tf_call -> hierarchical_identifier -> simple_identifier).+static char *first_leaf_identifier(CBMArena *a, TSNode node, const char *source) {+ TSNode cur = node;+ for (int depth = 0; depth < 8 && !ts_node_is_null(cur); depth++) {+ const char *k = ts_node_type(cur);+ if (strcmp(k, "simple_identifier") == 0 || strcmp(k, "identifier") == 0 ||+ strcmp(k, "word") == 0 || strcmp(k, "name") == 0 || strcmp(k, "qid") == 0) {+ char *t = cbm_node_text(a, cur, source);+ return (t && t[0]) ? t : NULL;+ }+ if (ts_node_named_child_count(cur) == 0) {+ return NULL;+ }+ cur = ts_node_named_child(cur, 0);+ }+ return NULL;+}++// Verilog / SystemVerilog: a function_subroutine_call wraps+// subroutine_call -> tf_call -> [hierarchical_identifier ->] simple_identifier.+// Descend to the first identifier leaf to name the callee.+static char *extract_hdl_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {+ if (strcmp(nk, "function_subroutine_call") != 0 && strcmp(nk, "subroutine_call") != 0 &&+ strcmp(nk, "tf_call") != 0 && strcmp(nk, "system_tf_call") != 0) {+ return NULL;+ }+ return first_leaf_identifier(a, node, source);+}++// VHDL: `add(x, 1)` parses as `(name (library_function) (parenthesis_group ...))`+// inside a `simple_expression` (the function-call / indexed-name ambiguity). The+// call_node_types set targets `parenthesis_group`; the callee is its immediately+// preceding named sibling (a `library_function`/`identifier`/`name` token).+static char *extract_vhdl_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {+ if (strcmp(nk, "parenthesis_group") != 0) {+ return NULL;+ }+ TSNode prev = ts_node_prev_named_sibling(node);+ if (ts_node_is_null(prev)) {+ return NULL;+ }+ const char *pk = ts_node_type(prev);+ if (strcmp(pk, "library_function") == 0 || strcmp(pk, "identifier") == 0 ||+ strcmp(pk, "name") == 0 || strcmp(pk, "simple_name") == 0) {+ char *t = cbm_node_text(a, prev, source);+ return (t && t[0]) ? t : NULL;+ }+ return NULL;+}++// NASM: a `call`/`jmp`-style instruction is an `actual_instruction` whose+// `instruction:` field is the mnemonic word and whose first operand word is the+// target label. Only treat call/jump mnemonics as calls; everything else (add,+// mov, ret, ...) is plain data-flow, not a call.+static char *extract_nasm_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {+ if (strcmp(nk, "actual_instruction") != 0) {+ return NULL;+ }+ TSNode mnem = ts_node_child_by_field_name(node, TS_FIELD("instruction"));+ if (ts_node_is_null(mnem)) {+ return NULL;+ }+ char *m = cbm_node_text(a, mnem, source);+ if (!m || (strcmp(m, "call") != 0 && strcmp(m, "jmp") != 0 && strcmp(m, "je") != 0 &&+ strcmp(m, "jne") != 0 && strcmp(m, "jz") != 0 && strcmp(m, "jnz") != 0)) {+ return NULL;+ }+ TSNode ops = ts_node_child_by_field_name(node, TS_FIELD("operands"));+ if (ts_node_is_null(ops) || ts_node_named_child_count(ops) == 0) {+ return NULL;+ }+ return first_leaf_identifier(a, ts_node_named_child(ops, 0), source);+}++// LLVM-IR: a `call`/`invoke` is an `instruction_call` whose `callee:` field is a+// `value -> var -> global_var` chain (e.g. `@inner`). Strip the leading sigil.+static char *extract_llvm_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {+ if (strcmp(nk, "instruction_call") != 0) {+ return NULL;+ }+ TSNode callee = ts_node_child_by_field_name(node, TS_FIELD("callee"));+ if (ts_node_is_null(callee)) {+ return NULL;+ }+ char *t = first_leaf_identifier(a, callee, source);+ if (!t) {+ t = cbm_node_text(a, callee, source);+ }+ if (t && (t[0] == '@' || t[0] == '%')) {+ return t + 1;+ }+ return t;+}++// FunC: a `function_application` carries the callee on its `function:` field.+static char *extract_func_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {+ if (strcmp(nk, "function_application") != 0) {+ return NULL;+ }+ TSNode fn = ts_node_child_by_field_name(node, TS_FIELD("function"));+ return ts_node_is_null(fn) ? NULL : cbm_node_text(a, fn, source);+}++// Agda: function application `f x y` parses as an `expr` whose named children are+// `atom`s (no dedicated application node). Treat an `expr` with >= 2 atom children+// as a call whose callee is the head atom's identifier.+static char *extract_agda_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {+ if (strcmp(nk, "expr") != 0 || ts_node_named_child_count(node) < 2) {+ return NULL;+ }+ TSNode head = ts_node_named_child(node, 0);+ if (strcmp(ts_node_type(head), "atom") != 0) {+ return NULL;+ }+ return first_leaf_identifier(a, head, source);+}++// Make: `$(shell ...)` is a `shell_function` node; the callee is the literal+// `shell` keyword. tree-sitter-make also exposes `function_call` for other+// builtins ($(wildcard ...), $(patsubst ...)).+static char *extract_make_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {+ if (strcmp(nk, "shell_function") == 0) {+ return cbm_arena_strndup(a, "shell", 5);+ }+ if (strcmp(nk, "function_call") == 0) {+ TSNode fn = ts_node_child_by_field_name(node, TS_FIELD("function"));+ if (ts_node_is_null(fn) && ts_node_named_child_count(node) > 0) {+ fn = ts_node_named_child(node, 0);+ }+ return ts_node_is_null(fn) ? NULL : cbm_node_text(a, fn, source);+ }+ return NULL;+}++// Just: a recipe dependency `recipe: dep` is a `dependency` node whose `name:`+// field is the referenced recipe.+static char *extract_just_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {+ if (strcmp(nk, "dependency") != 0) {+ return NULL;+ }+ TSNode name = ts_node_child_by_field_name(node, TS_FIELD("name"));+ if (ts_node_is_null(name) && ts_node_named_child_count(node) > 0) {+ name = ts_node_named_child(node, 0);+ }+ return ts_node_is_null(name) ? NULL : cbm_node_text(a, name, source);+}++// Puppet: `include foo` is an `include_statement`; the callee is the literal+// `include` keyword (the class/identifier args are resolved as separate refs).+static char *extract_puppet_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {+ if (strcmp(nk, "include_statement") == 0) {+ return cbm_arena_strndup(a, "include", 7);+ }+ if (strcmp(nk, "function_call") == 0) {+ if (ts_node_named_child_count(node) > 0) {+ TSNode head = ts_node_named_child(node, 0);+ if (strcmp(ts_node_type(head), "identifier") == 0) {+ return cbm_node_text(a, head, source);+ }+ }+ }+ return NULL;+}+static char *extract_callee_lang_specific(CBMArena *a, TSNode node, const char *source,CBMLanguage lang) {const char *nk = ts_node_type(node);@@ -865,6 +1032,60 @@ static char *extract_callee_lang_specific(CBMArena *a, TSNode node, const char *if (lang == CBM_LANG_SWIFT) {return extract_swift_callee(a, node, source, nk);}+ if (lang == CBM_LANG_VERILOG || lang == CBM_LANG_SYSTEMVERILOG) {+ char *c = extract_hdl_callee(a, node, source, nk);+ if (c) {+ return c;+ }+ }+ if (lang == CBM_LANG_VHDL) {+ char *c = extract_vhdl_callee(a, node, source, nk);+ if (c) {+ return c;+ }+ }+ if (lang == CBM_LANG_NASM) {+ char *c = extract_nasm_callee(a, node, source, nk);+ if (c) {+ return c;+ }+ }+ if (lang == CBM_LANG_LLVM_IR) {+ char *c = extract_llvm_callee(a, node, source, nk);+ if (c) {+ return c;+ }+ }+ if (lang == CBM_LANG_FUNC) {+ char *c = extract_func_callee(a, node, source, nk);+ if (c) {+ return c;+ }+ }+ if (lang == CBM_LANG_AGDA) {+ char *c = extract_agda_callee(a, node, source, nk);+ if (c) {+ return c;+ }+ }+ if (lang == CBM_LANG_MAKEFILE) {+ char *c = extract_make_callee(a, node, source, nk);+ if (c) {+ return c;+ }+ }+ if (lang == CBM_LANG_JUST) {+ char *c = extract_just_callee(a, node, source, nk);+ if (c) {+ return c;+ }+ }+ if (lang == CBM_LANG_PUPPET) {+ char *c = extract_puppet_callee(a, node, source, nk);+ if (c) {+ return c;+ }+ }return extract_scripting_callee(a, node, source, lang, nk);}diff --git a/internal/cbm/helpers.c b/internal/cbm/helpers.cindex 724a71eb2..1d89f4485 100644--- a/internal/cbm/helpers.c+++ b/internal/cbm/helpers.c@@ -146,6 +146,15 @@ static const char *generic_keywords[] = {"def", "fn", "func", "fun", "proc", "sub", "method", "async","await", "yield", NULL};+/* Puppet reserves control-flow words but NOT `include`/`require`/`contain`,+ * which are ordinary built-in functions invoked as calls. Using the generic+ * list would wrongly drop `include`/`require` call edges, so Puppet gets its+ * own reserved-word set that omits them. */+static const char *puppet_keywords[] = {+ "true", "false", "undef", "if", "elsif", "else", "unless", "case",+ "and", "or", "in", "node", "class", "define", "inherits", "default",+ "return", NULL};+bool cbm_is_keyword(const char *name, CBMLanguage lang) {if (!name || !name[0]) {return true;@@ -174,6 +183,9 @@ bool cbm_is_keyword(const char *name, CBMLanguage lang) {case CBM_LANG_KOTLIN:keywords = kotlin_keywords;break;+ case CBM_LANG_PUPPET:+ keywords = puppet_keywords;+ break;default:keywords = generic_keywords;break;diff --git a/internal/cbm/lang_specs.c b/internal/cbm/lang_specs.cindex 74ac85666..74263541a 100644--- a/internal/cbm/lang_specs.c+++ b/internal/cbm/lang_specs.c@@ -831,7 +831,7 @@ static const char *markdown_class_types[] = {"atx_heading", "setext_heading", NU// ==================== MAKEFILE ====================static const char *makefile_func_types[] = {"rule", "recipe", NULL};static const char *makefile_module_types[] = {"makefile", NULL};-static const char *makefile_call_types[] = {"function_call", "call", NULL};+static const char *makefile_call_types[] = {"function_call", "call", "shell_function", NULL};static const char *makefile_import_types[] = {"include_directive", "include", NULL};static const char *makefile_var_types[] = {"variable_assignment", NULL};@@ -978,7 +978,7 @@ static const char *d_throw_types[] = {"throw_expression", NULL};// ==================== LLVM IR ====================static const char *llvm_func_types[] = {"function_header", NULL};-static const char *llvm_call_types[] = {"call", "invoke", NULL};+static const char *llvm_call_types[] = {"call", "invoke", "instruction_call", NULL};static const char *llvm_branch_types[] = {"br", "switch", NULL};static const char *llvm_var_types[] = {"local_var", "global_var", NULL};@@ -1134,7 +1134,7 @@ static const char *ada_throw_types[] = {"raise_statement", NULL};static const char *ada_module_types[] = {"compilation", NULL};static const char *agda_func_types[] = {"function", NULL};static const char *agda_class_types[] = {"data", "record", NULL};-static const char *agda_call_types[] = {"module_application", NULL};+static const char *agda_call_types[] = {"module_application", "expr", NULL};static const char *agda_import_types[] = {"import", "open", "import_directive", "instance", NULL};static const char *agda_branch_types[] = {"lambda", "match", "do", NULL};static const char *agda_var_types[] = {"typed_binding", NULL};@@ -1261,7 +1261,7 @@ static const char *sway_assign_types[] = {"assignment_expression", NULL};static const char *sway_module_types[] = {"source_file", NULL};static const char *nasm_func_types[] = {"label", "preproc_def", "preproc_multiline_macro", NULL};static const char *nasm_class_types[] = {"struc_declaration", NULL};-static const char *nasm_call_types[] = {"call_syntax_expression", NULL};+static const char *nasm_call_types[] = {"call_syntax_expression", "actual_instruction", NULL};static const char *nasm_import_types[] = {"preproc_include", NULL};static const char *nasm_var_types[] = {"label", NULL};static const char *nasm_module_types[] = {"source_file", NULL};@@ -1271,7 +1271,7 @@ static const char *assembly_module_types[] = {"program", NULL};static const char *astro_module_types[] = {"document", NULL};static const char *blade_module_types[] = {"document", NULL};static const char *just_func_types[] = {"recipe", NULL};-static const char *just_call_types[] = {"function_call", NULL};+static const char *just_call_types[] = {"function_call", "dependency", NULL};static const char *just_import_types[] = {"import", NULL};static const char *just_branch_types[] = {"if_expression", NULL};static const char *just_assign_types[] = {"assignment", NULL};@@ -1385,7 +1385,8 @@ static const char *vhdl_class_types[] = {"interface_declaration", "package_declaration", "protected_type_declaration","record_type_definition", "type_declaration", NULL};static const char *vhdl_call_types[] = {"function_call", "procedure_call_statement",- "component_instantiation_statement", NULL};+ "component_instantiation_statement", "parenthesis_group",+ NULL};static const char *vhdl_import_types[] = {"library_clause", "use_clause", NULL};static const char *vhdl_branch_types[] = {"if_statement", "case_statement", "loop_statement", NULL};static const char *vhdl_var_types[] = {"variable_declaration", "signal_declaration",@@ -1490,7 +1491,7 @@ static const char *squirrel_assign_types[] = {"assignment_expression", NULL};static const char *squirrel_import_types[] = {"extends", NULL};static const char *squirrel_module_types[] = {"source_file", NULL};static const char *func_func_types[] = {"function_definition", NULL};-static const char *func_call_types[] = {"method_call", NULL};+static const char *func_call_types[] = {"method_call", "function_application", NULL};static const char *func_import_types[] = {"include_directive", NULL};static const char *func_module_types[] = {"source_file", NULL};static const char *regex_module_types[] = {"pattern", NULL};@@ -1502,7 +1503,8 @@ static const char *mermaid_module_types[] = {"source_file", NULL};static const char *puppet_func_types[] = {"function_declaration", "lambda", NULL};static const char *puppet_class_types[] = {"class_definition", "node_definition","resource_declaration", "type_declaration", NULL};-static const char *puppet_call_types[] = {"function_call", "resource_declaration", NULL};+static const char *puppet_call_types[] = {"function_call", "resource_declaration",+ "include_statement", NULL};static const char *puppet_import_types[] = {"include_statement", "require_statement", "include","require", NULL};static const char *puppet_branch_types[] = {"if_statement", "unless_statement", "case_statement",diff --git a/tests/repro/repro_grammar_build.c b/tests/repro/repro_grammar_build.cindex 3be78a421..82182209c 100644--- a/tests/repro/repro_grammar_build.c+++ b/tests/repro/repro_grammar_build.c@@ -606,16 +606,18 @@ TEST(repro_grammar_build_gn) {/* ── Just ─────────────────────────────────────────────────────────────────────* Idiomatic justfile with two recipes (just_func_types = {"recipe"} ->- * "Function") and a call from one recipe to a built-in (just_call_types =- * {"function_call"}). The `build` recipe calls `just_executable` via- * a recipe dependency. The `clean` recipe uses the shell idiom.+ * "Function") and a recipe dependency that the grammar encodes as a+ * `dependency` node (just_call_types includes "dependency"). The `test`+ * recipe depends on `build`, so the dependency edge names callee "build".+ * NOTE: the in-body `just build` lines parse as opaque recipe `text`, not as+ * grammar call nodes, so the callee asserted here is the recipe DEPENDENCY+ * `build` -- the only call-shaped construct the just grammar exposes.** Dims asserted: 1-8 (full battery).* Dim 5 expected GREEN: "Function" def for "build" and "test" recipes.* RED would indicate recipe->Function extraction is broken.- * Dim 6 expected GREEN: call to "env_var" or the recipe dependency call.- * If the just grammar encodes recipe dependencies as function_call nodes this- * should be GREEN; RED documents the gap.+ * Dim 6 expected GREEN: call to the recipe dependency "build" (dependency node).+ * RED documents the just dependency-as-call extraction gap.* Dim 7 expected RED: calls inside a recipe body are shell commands; the* enclosing-func walk looks for a parent node in func_kinds_for_lang, but* recipe body nodes (recipe_body / shell lines) are not typically in that@@ -643,7 +645,7 @@ TEST(repro_grammar_build_just) {" just test\n";static const char bad[] = "build:\n go build -o ";if (build_callable_battery("Just", src, CBM_LANG_JUST, "justfile",- "Function", "just") != 0)+ "Function", "build") != 0)return 1;if (build_robustness("Just", bad, CBM_LANG_JUST, "justfile") != 0)return 1;