- 在初始化解析中添加缺失的赋值操作符检查 - 改进后缀表达式解析逻辑,处理嵌套情况 - 添加数组下标初始化的赋值操作符验证 - 修复主表达式解析中的返回语句处理 refactor(pproc): 优化预处理器宏展开和位置追踪 - 添加token复制函数来保持原始位置信息 - 重构宏展开函数参数传递方式 - 修复字符串化参数的位置信息处理 - 改进可变参数宏的处理逻辑 test(parser): 增加标签语句和字符串字面量测试用例 - 添加返回语句with复合字面量的测试 - 增加标签继续语句的测试用例 - 添加字符串连接的解析测试 test(pproc): 添加预处理器位置追踪测试 - 增加双重宏定义位置追踪测试 - 添加带参数宏定义位置追踪测试 - 增加字符串化操作位置追踪测试 docs: 更新代码中的宏定义和注释 - 修正未定义标识符的拼写错误 - 添加必要的头文件包含 - 改进错误消息提示文本
139 lines
4.4 KiB
C
139 lines
4.4 KiB
C
#include <assert.h>
|
|
#include <scc_pproc.h>
|
|
#include <string.h>
|
|
#include <utest/acutest.h>
|
|
|
|
static void test_define_pos(void) {
|
|
int ret = 0;
|
|
scc_sstream_t mem_stream;
|
|
const char *input = "#define OBJ 1\nOBJ";
|
|
ret = scc_sstream_init_by_buffer(&mem_stream, input, strlen(input), false,
|
|
16);
|
|
Assert(ret == 0);
|
|
|
|
scc_lexer_t lexer;
|
|
scc_lexer_init(&lexer, scc_sstream_to_ring(&mem_stream));
|
|
|
|
scc_pproc_t pp;
|
|
scc_pproc_init(&pp, scc_lexer_to_ring(&lexer, 8, true));
|
|
|
|
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pp, 8, true, true);
|
|
scc_lexer_tok_t tok = {0};
|
|
|
|
scc_ring_next_consume(*tok_ring, tok, ret);
|
|
Assert(ret == true);
|
|
TEST_CHECK(tok.loc.line == 2 && tok.loc.col == 1 &&
|
|
scc_strcmp(tok.lexeme.data, "1") == 0);
|
|
TEST_MSG("Expected: %d:%d:%s", 2, 1, "1");
|
|
TEST_MSG("Produced: %zu:%zu:%s", tok.loc.line, tok.loc.col,
|
|
tok.lexeme.data);
|
|
|
|
scc_ring_free(*tok_ring);
|
|
scc_pproc_drop(&pp);
|
|
scc_lexer_drop(&lexer);
|
|
scc_sstream_drop(&mem_stream);
|
|
}
|
|
|
|
static void test_define_double_pos(void) {
|
|
int ret = 0;
|
|
scc_sstream_t mem_stream;
|
|
const char *input = "#define _OBJ 1\n#define OBJ _OBJ\nOBJ";
|
|
ret = scc_sstream_init_by_buffer(&mem_stream, input, strlen(input), false,
|
|
16);
|
|
Assert(ret == 0);
|
|
|
|
scc_lexer_t lexer;
|
|
scc_lexer_init(&lexer, scc_sstream_to_ring(&mem_stream));
|
|
|
|
scc_pproc_t pp;
|
|
scc_pproc_init(&pp, scc_lexer_to_ring(&lexer, 8, true));
|
|
|
|
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pp, 8, true, true);
|
|
scc_lexer_tok_t tok = {0};
|
|
|
|
scc_ring_next_consume(*tok_ring, tok, ret);
|
|
Assert(ret == true);
|
|
TEST_CHECK(tok.loc.line == 3 && tok.loc.col == 1 &&
|
|
scc_strcmp(tok.lexeme.data, "1") == 0);
|
|
TEST_MSG("Expected: %d:%d:%s", 3, 1, "1");
|
|
TEST_MSG("Produced: %zu:%zu:%s", tok.loc.line, tok.loc.col,
|
|
tok.lexeme.data);
|
|
|
|
scc_ring_free(*tok_ring);
|
|
scc_pproc_drop(&pp);
|
|
scc_lexer_drop(&lexer);
|
|
scc_sstream_drop(&mem_stream);
|
|
}
|
|
|
|
static void test_define_param_pos(void) {
|
|
int ret = 0;
|
|
scc_sstream_t mem_stream;
|
|
const char *input = "#define OBJ 1\n#define func(x) x\nfunc(OBJ)";
|
|
ret = scc_sstream_init_by_buffer(&mem_stream, input, strlen(input), false,
|
|
16);
|
|
Assert(ret == 0);
|
|
|
|
scc_lexer_t lexer;
|
|
scc_lexer_init(&lexer, scc_sstream_to_ring(&mem_stream));
|
|
|
|
scc_pproc_t pp;
|
|
scc_pproc_init(&pp, scc_lexer_to_ring(&lexer, 8, true));
|
|
|
|
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pp, 8, true, true);
|
|
scc_lexer_tok_t tok = {0};
|
|
|
|
scc_ring_next_consume(*tok_ring, tok, ret);
|
|
Assert(ret == true);
|
|
TEST_CHECK(tok.loc.line == 3 && tok.loc.col == 1 &&
|
|
scc_strcmp(tok.lexeme.data, "1") == 0);
|
|
TEST_MSG("Expected: %d:%d:%s", 3, 1, "1");
|
|
TEST_MSG("Produced: %zu:%zu:%s", tok.loc.line, tok.loc.col,
|
|
tok.lexeme.data);
|
|
|
|
scc_ring_free(*tok_ring);
|
|
scc_pproc_drop(&pp);
|
|
scc_lexer_drop(&lexer);
|
|
scc_sstream_drop(&mem_stream);
|
|
}
|
|
|
|
static void test_define_stringify_pos(void) {
|
|
int ret = 0;
|
|
scc_sstream_t mem_stream;
|
|
const char *input =
|
|
"#define _STR(x) #x\n#define STR(x) _STR(x)\n#define OBJ 1\nSTR(OBJ)";
|
|
ret = scc_sstream_init_by_buffer(&mem_stream, input, strlen(input), false,
|
|
16);
|
|
Assert(ret == 0);
|
|
|
|
scc_lexer_t lexer;
|
|
scc_lexer_init(&lexer, scc_sstream_to_ring(&mem_stream));
|
|
|
|
scc_pproc_t pp;
|
|
scc_pproc_init(&pp, scc_lexer_to_ring(&lexer, 8, true));
|
|
|
|
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pp, 8, true, true);
|
|
scc_lexer_tok_t tok = {0};
|
|
|
|
scc_ring_next_consume(*tok_ring, tok, ret);
|
|
Assert(ret == true);
|
|
TEST_CHECK(tok.loc.line == 4 && tok.loc.col == 1 &&
|
|
scc_strcmp(tok.lexeme.data, "\"1\"") == 0);
|
|
TEST_MSG("Expected: %d:%d:%s", 4, 1, "\"1\"");
|
|
TEST_MSG("Produced: %zu:%zu:%s", tok.loc.line, tok.loc.col,
|
|
tok.lexeme.data);
|
|
|
|
scc_ring_free(*tok_ring);
|
|
scc_pproc_drop(&pp);
|
|
scc_lexer_drop(&lexer);
|
|
scc_sstream_drop(&mem_stream);
|
|
}
|
|
|
|
#define TEST_LIST_CASE(func_name) {#func_name, func_name}
|
|
TEST_LIST = {
|
|
TEST_LIST_CASE(test_define_pos),
|
|
TEST_LIST_CASE(test_define_double_pos),
|
|
TEST_LIST_CASE(test_define_param_pos),
|
|
TEST_LIST_CASE(test_define_stringify_pos),
|
|
{NULL, NULL},
|
|
};
|