Skip to content

Commit 61de19b

Browse files
authored
Merge pull request DeusData#1940 from ilyabrykau-orca/fix/go-struct-fields
fix(extract): descend into Go struct field_declaration_list
2 parents fcd8bfc + e07a12b commit 61de19b

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4780,6 +4780,20 @@ static TSNode find_class_member_body(TSNode class_node, CBMLanguage lang) {
47804780
return ts_node_is_null(declarations) ? body : declarations;
47814781
}
47824782

4783+
/* Go structs keep their field_declaration nodes one level below the body that
4784+
* find_class_body() returns: type_spec's `type` child is a struct_type whose
4785+
* only named child is a field_declaration_list. Interfaces need no such step --
4786+
* interface_type holds its method specs directly, which is why interface members
4787+
* extracted correctly while every struct field was silently skipped. Normalize
4788+
* here, the same way the Java enum_body_declarations step above does. */
4789+
static TSNode go_normalize_struct_body(TSNode body) {
4790+
if (ts_node_is_null(body) || strcmp(ts_node_type(body), "struct_type") != 0) {
4791+
return body;
4792+
}
4793+
TSNode list = cbm_find_child_by_kind(body, "field_declaration_list");
4794+
return ts_node_is_null(list) ? body : list;
4795+
}
4796+
47834797
// Dart: resolve method name from method_signature/function_signature.
47844798
static TSNode resolve_dart_method_name(TSNode child, const char *ck) {
47854799
if (strcmp(ck, "method_signature") == 0) {
@@ -6656,6 +6670,9 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const ch
66566670
}
66576671

66586672
TSNode body = find_class_member_body(class_node, ctx->language);
6673+
if (ctx->language == CBM_LANG_GO) {
6674+
body = go_normalize_struct_body(body);
6675+
}
66596676
if (ts_node_is_null(body)) {
66606677
return;
66616678
}
@@ -6945,6 +6962,13 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const ch
69456962
continue;
69466963
}
69476964

6965+
/* Go: `_` is the blank identifier, used for explicit struct padding in
6966+
* generated code. It is not a referenceable field, and emitting it gives
6967+
* every `_` in the repository a same-named node to collide with. */
6968+
if (ctx->language == CBM_LANG_GO && strcmp(name, "_") == 0) {
6969+
continue;
6970+
}
6971+
69486972
const char *field_qn = cbm_arena_sprintf(a, "%s.%s", class_qn, name);
69496973

69506974
CBMDefinition def;

‎tests/test_extraction.c‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,6 +3015,59 @@ TEST(go_imports) {
30153015
PASS();
30163016
}
30173017

3018+
/* #1935: Go struct fields were never extracted — find_class_body() returns the
3019+
* struct_type node, whose only named child is a field_declaration_list, so the
3020+
* member loop matched nothing and every field was silently skipped (0 Field
3021+
* nodes for ~5055 declarations on the measured repo). Interfaces hold their
3022+
* method specs directly and always worked. The blank identifier `_` is struct
3023+
* padding, not a referenceable field, and must stay out (241 collision edges
3024+
* on two generated structs otherwise). */
3025+
TEST(extract_go_struct_fields_have_nodes) {
3026+
CBMFileResult *r = extract("package fxf\n\n"
3027+
"type Config struct {\n"
3028+
"\tName string\n"
3029+
"\tTimeout int\n"
3030+
"\tNested *Config\n"
3031+
"\t_ [8]byte\n"
3032+
"}\n\n"
3033+
"type Reader interface {\n"
3034+
"\tRead(p []byte) (int, error)\n"
3035+
"\tClose() error\n"
3036+
"}\n",
3037+
CBM_LANG_GO, "t", "cfg.go");
3038+
ASSERT_NOT_NULL(r);
3039+
ASSERT_FALSE(r->has_error);
3040+
/* RED before the descend fix: count is 0. The blank identifier must not
3041+
* bring it to 4. */
3042+
ASSERT_EQ(count_defs_with_label(r, "Field"), 3);
3043+
ASSERT_TRUE(has_def(r, "Field", "Name"));
3044+
ASSERT_TRUE(has_def(r, "Field", "Timeout"));
3045+
ASSERT_TRUE(has_def(r, "Field", "Nested"));
3046+
ASSERT_FALSE(has_def(r, "Field", "_"));
3047+
/* Each field carries its declared type in return_type. */
3048+
for (int i = 0; i < r->defs.count; i++) {
3049+
const CBMDefinition *d = &r->defs.items[i];
3050+
if (!d->label || strcmp(d->label, "Field") != 0) {
3051+
continue;
3052+
}
3053+
ASSERT_NOT_NULL(d->return_type);
3054+
if (strcmp(d->name, "Name") == 0) {
3055+
ASSERT_TRUE(strcmp(d->return_type, "string") == 0);
3056+
}
3057+
if (strcmp(d->name, "Timeout") == 0) {
3058+
ASSERT_TRUE(strcmp(d->return_type, "int") == 0);
3059+
}
3060+
if (strcmp(d->name, "Nested") == 0) {
3061+
ASSERT_TRUE(strcmp(d->return_type, "*Config") == 0);
3062+
}
3063+
}
3064+
/* Interface members keep extracting exactly as before. */
3065+
ASSERT_TRUE(has_def(r, "Method", "Read"));
3066+
ASSERT_TRUE(has_def(r, "Method", "Close"));
3067+
cbm_free_result(r);
3068+
PASS();
3069+
}
3070+
30183071
TEST(java_imports) {
30193072
CBMFileResult *r = extract(
30203073
"import java.util.List;\nimport java.util.ArrayList;\nimport static java.lang.Math.PI;\n"
@@ -6699,6 +6752,7 @@ SUITE(extraction) {
66996752
RUN_TEST(python_imports);
67006753
RUN_TEST(js_imports);
67016754
RUN_TEST(go_imports);
6755+
RUN_TEST(extract_go_struct_fields_have_nodes);
67026756
RUN_TEST(java_imports);
67036757
RUN_TEST(rust_imports);
67046758
RUN_TEST(c_imports);

0 commit comments

Comments
 (0)