- 添加了命令行参数约束相关数据结构定义 - 新增错误上下文字段用于更好的错误提示 - 实现了参数验证功能,包括必需参数检查和值选择验证 - 改进错误处理流程,支持添加帮助信息和使用说明 - 优化调试输出格式,增加更多错误场景的处理 fix(parser): 修复语义分析器资源释放问题 - 在scc_sema_drop函数中添加空指针检查,避免空指针释放导致崩溃 refactor(dump): 重构AST节点转储实现 - 将树形转储上下文重构为更简洁的数据结构 - 修改转储回调函数签名以支持更好的缓冲区管理 - 优化内存拷贝操作,提高转储性能 style(amd64): 移除未使用的变量声明 - 删除scc_mcode_amd64_mov_r64_m64_sib函数中未使用的disp8变量
94 lines
3.4 KiB
C
94 lines
3.4 KiB
C
#include <scc_lexer.h>
|
|
#include <scc_parser.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <utest/acutest.h>
|
|
|
|
typedef scc_ast_node_t *(*scc_parse_node_func)(scc_parser_t *parser);
|
|
|
|
static scc_ast_node_t *process_input(const char *input,
|
|
scc_parse_node_func parse_func,
|
|
cbool need_sema) {
|
|
int res = 0;
|
|
scc_sstream_t mem_stream;
|
|
res = scc_sstream_init_by_buffer(&mem_stream, input, strlen(input), false,
|
|
16);
|
|
Assert(res == 0);
|
|
|
|
scc_lexer_t lexer;
|
|
scc_lexer_init(&lexer, scc_sstream_to_ring(&mem_stream));
|
|
|
|
scc_lexer_tok_ring_t *tok_ring = scc_lexer_to_ring(&lexer, 64, false);
|
|
|
|
scc_parser_t parser;
|
|
if (need_sema) {
|
|
scc_sema_callbacks_t sema_callbacks;
|
|
scc_sema_init(&sema_callbacks);
|
|
scc_parser_init(&parser, tok_ring, &sema_callbacks);
|
|
} else {
|
|
scc_parser_init(&parser, tok_ring, nullptr);
|
|
}
|
|
|
|
scc_ast_node_t *ret = parse_func(&parser);
|
|
|
|
cbool not_eof = false;
|
|
scc_ring_not_eof(*parser.ring, not_eof);
|
|
if (not_eof == true) {
|
|
// FIXME MAYBE free
|
|
LOG_FATAL("Didn't consume all tokens");
|
|
return nullptr;
|
|
}
|
|
|
|
scc_lexer_drop_ring(parser.ring);
|
|
scc_parser_drop(&parser);
|
|
scc_lexer_drop(&lexer);
|
|
scc_sstream_drop(&mem_stream);
|
|
return ret;
|
|
}
|
|
|
|
typedef void (*scc_tree_dump_output_t)(void *userdata, const char *fmt, ...);
|
|
|
|
#define BUFFER_SIZE (4096)
|
|
char expect_buffer[BUFFER_SIZE];
|
|
char output_buffer[BUFFER_SIZE];
|
|
|
|
static void dump2buffer(const char *str, usize len, void *user) {
|
|
char *buff = user;
|
|
buff[0] = '\n';
|
|
buff[1] = '\0';
|
|
memcpy(buff, str, len + 1);
|
|
}
|
|
|
|
static void _scc_check_ast(scc_ast_node_t *expect_node_ptr, const char *str,
|
|
scc_parse_node_func parse_func, cbool need_sema) {
|
|
scc_ast_node_t *output_node_ptr = process_input(str, parse_func, need_sema);
|
|
scc_tree_dump_t ctx;
|
|
scc_tree_dump_init(&ctx, true);
|
|
scc_ast_dump_node(&ctx, expect_node_ptr);
|
|
scc_tree_dump_flush(&ctx, dump2buffer, expect_buffer);
|
|
scc_tree_dump_drop(&ctx);
|
|
|
|
scc_tree_dump_init(&ctx, true);
|
|
scc_ast_dump_node(&ctx, output_node_ptr);
|
|
scc_tree_dump_flush(&ctx, dump2buffer, output_buffer);
|
|
scc_tree_dump_drop(&ctx);
|
|
}
|
|
|
|
#define SCC_CHECK_AST_WITH_SEMA(expect_node_ptr, str, parse_func) \
|
|
do { \
|
|
_scc_check_ast(expect_node_ptr, str, (scc_parse_node_func)parse_func, \
|
|
true); \
|
|
TEST_CHECK(strcmp(output_buffer, expect_buffer) == 0); \
|
|
TEST_MSG("Expected: %s", expect_buffer); \
|
|
TEST_MSG("Produced: %s", output_buffer); \
|
|
} while (0);
|
|
|
|
#define SCC_CHECK_AST(expect_node_ptr, str, parse_func) \
|
|
do { \
|
|
_scc_check_ast(expect_node_ptr, str, (scc_parse_node_func)parse_func, \
|
|
false); \
|
|
TEST_CHECK(strcmp(output_buffer, expect_buffer) == 0); \
|
|
TEST_MSG("Expected: %s", expect_buffer); \
|
|
TEST_MSG("Produced: %s", output_buffer); \
|
|
} while (0);
|