fix(ast): 修复AST转储中逗号分隔符逻辑错误

在dump_decl_impl函数中,修正了列表元素间逗号分隔符的判断逻辑,
将条件从"不是最后一个元素"改为"是最后一个元素",确保正确的
分隔符输出。

BREAKING CHANGE: 逗号分隔符的逻辑被反转,影响AST转储输出格式。

---

fix(parser): 修复声明列表处理中的内存泄漏问题

调整了typedef声明的处理顺序,在解析完成前添加声明到列表,
避免了重复添加导致的内存泄漏。同时移除了不必要的错误检查。

---

refactor(parser): 重构记录类型和枚举类型的声明初始化逻辑

移除了重复的声明创建代码,统一了结构体、联合体和枚举类型的
声明初始化流程,消除了之前存在的Panic错误路径。

---

test(parser): 增加复杂声明的单元测试覆盖

添加了对结构体指针声明和typedef复合声明的测试用例,
提高了测试覆盖率并验证了解析器的正确性。

---

feat(preprocessor): 添加预定义宏支持

增加了__SCC__、_WIN64和__x86_64__等预定义宏的定义,
为不同平台提供更好的编译支持。
This commit is contained in:
zzy
2026-03-14 15:39:26 +08:00
parent eb969cdbf7
commit 82dd5f2db0
5 changed files with 122 additions and 95 deletions

View File

@@ -545,7 +545,7 @@ static void test_parser_unit(void) {
// 6. 函数声明
scc_ast_decl_t decl;
scc_ast_decl_func_init(&decl, &func_type, "call", NULL);
scc_ast_decl_func_init(&decl, &func_type, "call", null);
// 7. 与解析结果比较
SCC_CHECK_AST_WITH_SEMA(&decl.base,
@@ -735,8 +735,81 @@ static void test_parser_unit(void) {
SCC_CHECK_AST(&decl_list.base, "int a = 1, b = 2;",
scc_parse_declaration);
}
"struct list_head *next, *prev;";
"typedef struct { int a; } struct_t, *struct_ptr_t;";
// 测试 "struct list_head *next, *prev;"
{
// 构造 struct list_head 类型(不完整)
scc_ast_type_t struct_list_head;
scc_ast_type_struct_init(&struct_list_head, "list_head", null);
// 构造两个指针类型(分别用于 next 和 prev指向同一结构体
scc_ast_type_t ptr_to_struct1, ptr_to_struct2;
scc_ast_type_pointer_init(&ptr_to_struct1, &struct_list_head);
scc_ast_type_pointer_init(&ptr_to_struct2, &struct_list_head);
// 构造变量声明 next 和 prev
scc_ast_decl_t next_decl, prev_decl;
scc_ast_decl_val_init(&next_decl, &ptr_to_struct1, "next", null);
scc_ast_decl_val_init(&prev_decl, &ptr_to_struct2, "prev", null);
// 构造声明列表
scc_ast_decl_vec_t decl_vec;
scc_vec_init(decl_vec);
scc_vec_push(decl_vec, &next_decl);
scc_vec_push(decl_vec, &prev_decl);
scc_ast_decl_t decl_list;
scc_ast_decl_list_init(&decl_list, &decl_vec);
SCC_CHECK_AST(&decl_list.base, "struct list_head *next, *prev;",
scc_parse_declaration);
}
// 测试 "typedef struct { int a; } struct_t, *struct_ptr_t;"
{
// 构造字段 int a;
scc_ast_decl_t field_a;
scc_ast_decl_val_init(&field_a,
(scc_ast_type_t *)&scc_ast_builtin_type_int,
"a", null);
scc_ast_decl_vec_t fields;
scc_vec_init(fields);
scc_vec_push(fields, &field_a);
// 构造匿名结构体定义声明
scc_ast_decl_t struct_def;
scc_ast_decl_struct_init(&struct_def, null,
&fields); // fields 被移动
// 构造匿名结构体类型
scc_ast_type_t anon_struct_type;
scc_ast_type_struct_init(&anon_struct_type, null, &struct_def);
// 构造指针类型指向该匿名结构体
scc_ast_type_t ptr_to_anon;
scc_ast_type_pointer_init(&ptr_to_anon, &anon_struct_type);
// 构造 typedef 声明 struct_t
scc_ast_decl_t typedef_struct_t;
scc_ast_decl_typedef_init(&typedef_struct_t, "struct_t",
&anon_struct_type);
// 构造 typedef 声明 struct_ptr_t
scc_ast_decl_t typedef_struct_ptr_t;
scc_ast_decl_typedef_init(&typedef_struct_ptr_t, "struct_ptr_t",
&ptr_to_anon);
// 构造声明列表
scc_ast_decl_vec_t typedef_vec;
scc_vec_init(typedef_vec);
scc_vec_push(typedef_vec, &typedef_struct_t);
scc_vec_push(typedef_vec, &typedef_struct_ptr_t);
scc_ast_decl_t decl_list;
scc_ast_decl_list_init(&decl_list, &typedef_vec);
SCC_CHECK_AST(&decl_list.base,
"typedef struct { int a; } struct_t, *struct_ptr_t;",
scc_parse_declaration);
}
"__scc_builtin_va_arg(ag, int)";
"__scc_builtin_va_arg(ag, long long)";
"typedef struct a;int a;struct a b;";