feat(pproc): 改进宏处理器中的标识符识别逻辑

修复了宏处理器中对标识符类型的检查方式,使用更准确的子类型判断函数,
确保关键字也能被正确处理为宏名或参数名。同时添加了相关的单元测试用例。

BREAKING CHANGE: 修改了token类型的检查方法
This commit is contained in:
zzy
2026-03-04 17:35:54 +08:00
parent 4015acd866
commit a805814d3f
7 changed files with 42 additions and 15 deletions

View File

@@ -1,9 +1,22 @@
lexer 词法分析
parse 语法分析
pproc 预处理器
parser 语法分析 sema 语义分析
ast 抽象语法树
sema 语义分析
ir 中间代码标识
- ast2ir
- ir2mcode
opt 优化器
codegen 代码生成
mcode 机器码
sccf 统一输出格式
target 目标平台支持

View File

@@ -119,7 +119,8 @@ void scc_pproc_parse_function_macro(scc_pproc_t *pp,
if (idx++ % 2 != 1) {
LOG_FATAL("ERROR");
}
} else if (arg->type == SCC_TOK_IDENT) {
} else if (scc_get_tok_subtype(arg->type) ==
SCC_TOK_SUBTYPE_IDENTIFIER) {
if (idx++ % 2 != 0) {
LOG_FATAL("ERROR");
}

View File

@@ -52,7 +52,7 @@ CONTINUE:
scc_ring_next_consume(*stream, *out, ok);
pp->at_line_start = true;
return true;
} else if (tok.type == SCC_TOK_IDENT) {
} else if (scc_get_tok_subtype(tok.type) == SCC_TOK_SUBTYPE_IDENTIFIER) {
// maybe expanded
scc_pproc_macro_t *macro =
scc_pproc_macro_table_get(&pp->macro_table, &tok.lexeme);

View File

@@ -181,6 +181,19 @@ static void test_define_nested_macros(void) {
"#define x 2\n"
"g(z)\n",
"f(2 * (z))\n");
TEST_CASE("with keyword as macro name or parameter name");
CHECK_PP_OUTPUT_EXACT(
"#define ASSIGN_PTR_OR_DEFAULT(assigned_val, value, default)\\\n"
"assigned_val = value ? value : default\n"
"ASSIGN_PTR_OR_DEFAULT(parser->sema_callbacks.on_decl,"
"callbacks->on_decl, dummy_sema_callback);\n",
"parser->sema_callbacks.on_decl = callbacks->on_decl ? "
"callbacks->on_decl : dummy_sema_callback;\n");
CHECK_PP_OUTPUT_EXACT("#define default a\n"
"default\n",
"a\n");
}
static void test_undef_macros(void) {