Skip to content

Commit ec70bfd

Browse files
committed
Serialize param_names/param_types to graph properties (Phase B1)
Function/Method nodes now include param_names and param_types arrays in their properties JSON. Data was already extracted by tree-sitter but not serialized. - append_json_str_array helper replaces inline decorator serialization - build_def_props in both pass_parallel.c and pass_definitions.c - extract_param_names: add Python default_parameter, typed_default_parameter, bare identifier node types - Sample project: 114 Python functions now have param_names - Zero extraction overhead, ~3MB more in properties for large projects
1 parent c02d3c9 commit ec70bfd

3 files changed

Lines changed: 92 additions & 74 deletions

File tree

internal/cbm/extract_defs.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,10 +749,16 @@ static const char **extract_param_names(CBMArena *a, TSNode params, const char *
749749
name_text = cbm_node_text(a, nm, source);
750750
}
751751
}
752+
// Python: identifier is a bare parameter name (def f(x, y))
753+
else if (strcmp(pk, "identifier") == 0) {
754+
name_text = cbm_node_text(a, param, source);
755+
}
752756
// Generic: try "name" field on parameter nodes
753757
else if (strcmp(pk, "formal_parameter") == 0 || strcmp(pk, "parameter") == 0 ||
754758
strcmp(pk, "required_parameter") == 0 || strcmp(pk, "optional_parameter") == 0 ||
755-
strcmp(pk, "simple_parameter") == 0 || strcmp(pk, "typed_parameter") == 0) {
759+
strcmp(pk, "simple_parameter") == 0 || strcmp(pk, "typed_parameter") == 0 ||
760+
strcmp(pk, "default_parameter") == 0 ||
761+
strcmp(pk, "typed_default_parameter") == 0) {
756762
TSNode nm = ts_node_child_by_field_name(param, "name", 4);
757763
if (ts_node_is_null(nm)) {
758764
nm = ts_node_child_by_field_name(param, "pattern", 7);

src/pipeline/pass_definitions.c

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,46 @@ static void append_json_string(char *buf, size_t bufsize, size_t *pos, const cha
128128
*pos = p;
129129
}
130130

131-
/* Build properties JSON for a definition node.
132-
* Includes decorators array, docstring, and signature when present. */
131+
/* Append a JSON array of strings: ,"key":["a","b","c"] */
132+
static void append_json_str_array(char *buf, size_t bufsize, size_t *pos, const char *key,
133+
const char **arr) {
134+
if (!arr || !arr[0] || *pos >= bufsize - 10) {
135+
return;
136+
}
137+
size_t p = *pos;
138+
int n = snprintf(buf + p, bufsize - p, ",\"%s\":[", key);
139+
if (n <= 0 || p + (size_t)n >= bufsize - 2) {
140+
return;
141+
}
142+
p += (size_t)n;
143+
for (int i = 0; arr[i]; i++) {
144+
if (i > 0 && p < bufsize - 1) {
145+
buf[p++] = ',';
146+
}
147+
if (p < bufsize - 1) {
148+
buf[p++] = '"';
149+
}
150+
for (const char *s = arr[i]; *s && p < bufsize - 2; s++) {
151+
if (*s == '"' || *s == '\\') {
152+
buf[p++] = '\\';
153+
if (p >= bufsize - 2) {
154+
break;
155+
}
156+
}
157+
buf[p++] = *s;
158+
}
159+
if (p < bufsize - 1) {
160+
buf[p++] = '"';
161+
}
162+
}
163+
if (p < bufsize - 1) {
164+
buf[p++] = ']';
165+
}
166+
buf[p] = '\0';
167+
*pos = p;
168+
}
169+
170+
/* Build properties JSON for a definition node. */
133171
static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def) {
134172
int n = snprintf(buf, bufsize,
135173
"{\"complexity\":%d,\"lines\":%d,\"is_exported\":%s,"
@@ -142,48 +180,12 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
142180
return;
143181
}
144182
size_t pos = (size_t)n;
145-
146-
/* Append docstring if present */
147183
append_json_string(buf, bufsize, &pos, "docstring", def->docstring);
148-
149-
/* Append signature if present */
150184
append_json_string(buf, bufsize, &pos, "signature", def->signature);
185+
append_json_str_array(buf, bufsize, &pos, "decorators", def->decorators);
186+
append_json_str_array(buf, bufsize, &pos, "param_names", def->param_names);
187+
append_json_str_array(buf, bufsize, &pos, "param_types", def->param_types);
151188

152-
/* Append decorators array if present */
153-
if (def->decorators && def->decorators[0] && pos < bufsize - 2) {
154-
const char *prefix = ",\"decorators\":[";
155-
size_t plen = strlen(prefix);
156-
if (pos + plen < bufsize) {
157-
memcpy(buf + pos, prefix, plen);
158-
pos += plen;
159-
}
160-
for (int i = 0; def->decorators[i]; i++) {
161-
if (i > 0 && pos < bufsize - 1) {
162-
buf[pos++] = ',';
163-
}
164-
if (pos < bufsize - 1) {
165-
buf[pos++] = '"';
166-
}
167-
for (const char *s = def->decorators[i]; *s && pos < bufsize - 2; s++) {
168-
if (*s == '"' || *s == '\\') {
169-
buf[pos++] = '\\';
170-
if (pos >= bufsize - 2) {
171-
break;
172-
}
173-
}
174-
buf[pos++] = *s;
175-
}
176-
if (pos < bufsize - 1) {
177-
buf[pos++] = '"';
178-
}
179-
}
180-
if (pos < bufsize - 1) {
181-
buf[pos++] = ']';
182-
}
183-
buf[pos] = '\0';
184-
}
185-
186-
/* Close the object */
187189
if (pos < bufsize - 1) {
188190
buf[pos] = '}';
189191
buf[pos + 1] = '\0';

src/pipeline/pass_parallel.c

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,45 @@ static void append_json_string(char *buf, size_t bufsize, size_t *pos, const cha
141141
*pos = p;
142142
}
143143

144+
/* Append a JSON array of strings: ,"key":["a","b","c"] */
145+
static void append_json_str_array(char *buf, size_t bufsize, size_t *pos, const char *key,
146+
const char **arr) {
147+
if (!arr || !arr[0] || *pos >= bufsize - 10) {
148+
return;
149+
}
150+
size_t p = *pos;
151+
int n = snprintf(buf + p, bufsize - p, ",\"%s\":[", key);
152+
if (n <= 0 || p + (size_t)n >= bufsize - 2) {
153+
return;
154+
}
155+
p += (size_t)n;
156+
for (int i = 0; arr[i]; i++) {
157+
if (i > 0 && p < bufsize - 1) {
158+
buf[p++] = ',';
159+
}
160+
if (p < bufsize - 1) {
161+
buf[p++] = '"';
162+
}
163+
for (const char *s = arr[i]; *s && p < bufsize - 2; s++) {
164+
if (*s == '"' || *s == '\\') {
165+
buf[p++] = '\\';
166+
if (p >= bufsize - 2) {
167+
break;
168+
}
169+
}
170+
buf[p++] = *s;
171+
}
172+
if (p < bufsize - 1) {
173+
buf[p++] = '"';
174+
}
175+
}
176+
if (p < bufsize - 1) {
177+
buf[p++] = ']';
178+
}
179+
buf[p] = '\0';
180+
*pos = p;
181+
}
182+
144183
static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def) {
145184
int n = snprintf(buf, bufsize,
146185
"{\"complexity\":%d,\"lines\":%d,\"is_exported\":%s,"
@@ -154,38 +193,9 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
154193
size_t pos = (size_t)n;
155194
append_json_string(buf, bufsize, &pos, "docstring", def->docstring);
156195
append_json_string(buf, bufsize, &pos, "signature", def->signature);
157-
if (def->decorators && def->decorators[0] && pos < bufsize - 2) {
158-
const char *prefix = ",\"decorators\":[";
159-
size_t plen = strlen(prefix);
160-
if (pos + plen < bufsize) {
161-
memcpy(buf + pos, prefix, plen);
162-
pos += plen;
163-
}
164-
for (int i = 0; def->decorators[i]; i++) {
165-
if (i > 0 && pos < bufsize - 1) {
166-
buf[pos++] = ',';
167-
}
168-
if (pos < bufsize - 1) {
169-
buf[pos++] = '"';
170-
}
171-
for (const char *s = def->decorators[i]; *s && pos < bufsize - 2; s++) {
172-
if (*s == '"' || *s == '\\') {
173-
buf[pos++] = '\\';
174-
if (pos >= bufsize - 2) {
175-
break;
176-
}
177-
}
178-
buf[pos++] = *s;
179-
}
180-
if (pos < bufsize - 1) {
181-
buf[pos++] = '"';
182-
}
183-
}
184-
if (pos < bufsize - 1) {
185-
buf[pos++] = ']';
186-
}
187-
buf[pos] = '\0';
188-
}
196+
append_json_str_array(buf, bufsize, &pos, "decorators", def->decorators);
197+
append_json_str_array(buf, bufsize, &pos, "param_names", def->param_names);
198+
append_json_str_array(buf, bufsize, &pos, "param_types", def->param_types);
189199
if (pos < bufsize - 1) {
190200
buf[pos] = '}';
191201
buf[pos + 1] = '\0';

0 commit comments

Comments
 (0)