From a805814d3f6dd7d0a242168cc3b8e1c446a67142 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Wed, 4 Mar 2026 17:35:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(pproc):=20=E6=94=B9=E8=BF=9B=E5=AE=8F?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=99=A8=E4=B8=AD=E7=9A=84=E6=A0=87=E8=AF=86?= =?UTF-8?q?=E7=AC=A6=E8=AF=86=E5=88=AB=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复了宏处理器中对标识符类型的检查方式,使用更准确的子类型判断函数, 确保关键字也能被正确处理为宏名或参数名。同时添加了相关的单元测试用例。 BREAKING CHANGE: 修改了token类型的检查方法 --- libs/README.md | 19 +++++++++++++++--- libs/pproc/src/pproc_directive.c | 3 ++- libs/pproc/src/scc_pproc.c | 2 +- libs/pproc/tests/test_pproc_unit.c | 13 ++++++++++++ .../include/{printf => scc_printf}/printf.c | 0 .../include/{printf => scc_printf}/printf.h | 0 runtime/scc_core/src/core_impl.c | 20 +++++++++---------- 7 files changed, 42 insertions(+), 15 deletions(-) rename runtime/scc_core/include/{printf => scc_printf}/printf.c (100%) rename runtime/scc_core/include/{printf => scc_printf}/printf.h (100%) diff --git a/libs/README.md b/libs/README.md index 9b4b272..43e35e7 100644 --- a/libs/README.md +++ b/libs/README.md @@ -1,9 +1,22 @@ lexer 词法分析 -parse 语法分析 + +pproc 预处理器 + +parser 语法分析 sema 语义分析 + ast 抽象语法树 -sema 语义分析 + ir 中间代码标识 + +- ast2ir + +- ir2mcode + opt 优化器 -codegen 代码生成 + +mcode 机器码 + +sccf 统一输出格式 + target 目标平台支持 diff --git a/libs/pproc/src/pproc_directive.c b/libs/pproc/src/pproc_directive.c index 549c7d1..95122c6 100644 --- a/libs/pproc/src/pproc_directive.c +++ b/libs/pproc/src/pproc_directive.c @@ -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"); } diff --git a/libs/pproc/src/scc_pproc.c b/libs/pproc/src/scc_pproc.c index 6ac163a..9575f06 100644 --- a/libs/pproc/src/scc_pproc.c +++ b/libs/pproc/src/scc_pproc.c @@ -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); diff --git a/libs/pproc/tests/test_pproc_unit.c b/libs/pproc/tests/test_pproc_unit.c index 5548e05..df4bea3 100644 --- a/libs/pproc/tests/test_pproc_unit.c +++ b/libs/pproc/tests/test_pproc_unit.c @@ -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) { diff --git a/runtime/scc_core/include/printf/printf.c b/runtime/scc_core/include/scc_printf/printf.c similarity index 100% rename from runtime/scc_core/include/printf/printf.c rename to runtime/scc_core/include/scc_printf/printf.c diff --git a/runtime/scc_core/include/printf/printf.h b/runtime/scc_core/include/scc_printf/printf.h similarity index 100% rename from runtime/scc_core/include/printf/printf.h rename to runtime/scc_core/include/scc_printf/printf.h diff --git a/runtime/scc_core/src/core_impl.c b/runtime/scc_core/src/core_impl.c index 31e59d3..4c8d5a0 100644 --- a/runtime/scc_core/src/core_impl.c +++ b/runtime/scc_core/src/core_impl.c @@ -1,5 +1,5 @@ // IMPLEMENT -#include +#include #include #define __SCC_LOG_IMPL_IMPORT_SRC__ @@ -16,15 +16,7 @@ scc_file_t scc_fopen(const char *path, scc_fmode_t mode) { void scc_fclose(scc_file_t file) { scc_pal_fclose(file); } -usize scc_fread(scc_file_t file, void *buffer, usize size) { - return scc_pal_fread(file, buffer, size); -} - -usize scc_fwrite(scc_file_t file, const void *buffer, usize size) { - return scc_pal_fwrite(file, buffer, size); -} - -usize scc_fsize(scc_pal_file_t file) { +usize scc_fsize(scc_file_t file) { if (scc_pal_fseek(file, 0, SCC_SEEK_PAL_END) != 0) { LOG_ERROR("fseek failed"); return 0; @@ -37,6 +29,14 @@ usize scc_fsize(scc_pal_file_t file) { return fsize; } +usize scc_fread(scc_file_t file, void *buffer, usize size) { + return scc_pal_fread(file, buffer, size); +} + +usize scc_fwrite(scc_file_t file, const void *buffer, usize size) { + return scc_pal_fwrite(file, buffer, size); +} + cbool scc_fexists(const char *path) { scc_file_t fp = scc_fopen(path, SCC_FILE_READ); if (!fp)