From c86071416db62d3facc0e0298e21f100a19cfa89 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Wed, 18 Feb 2026 18:18:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(pproc):=20=E5=AE=9E=E7=8E=B0=E5=AE=8F?= =?UTF-8?q?=E5=B1=95=E5=BC=80=E5=8A=9F=E8=83=BD=E5=B9=B6=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=AE=8F=E5=AE=9A=E4=B9=89=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 pproc_expand.h 头文件,定义宏展开相关的数据结构和函数接口 - 重命名宏相关类型和函数,将 scc_pp_* 前缀统一改为 scc_pproc_* - 修改宏参数解析逻辑,支持更灵活的参数处理方式 - 实现完整的宏展开功能,包括对象宏和函数宏的展开 - 添加字符串化操作符 (#) 的支持 - 改进预处理器主循环逻辑,优化宏展开流程 - 更新单元测试用例,增加对宏参数解析和字符串化的测试 --- libs/pproc/include/pproc_expand.h | 26 +++ libs/pproc/include/pproc_macro.h | 56 +++--- libs/pproc/include/scc_pproc.h | 15 +- libs/pproc/src/pproc_directive.c | 58 +++--- libs/pproc/src/pproc_expand.c | 283 +++++++++++++++++++++++++++--- libs/pproc/src/pproc_macro.c | 58 +++--- libs/pproc/src/scc_pproc.c | 50 ++++-- libs/pproc/tests/test_unit.c | 5 +- 8 files changed, 419 insertions(+), 132 deletions(-) create mode 100644 libs/pproc/include/pproc_expand.h diff --git a/libs/pproc/include/pproc_expand.h b/libs/pproc/include/pproc_expand.h new file mode 100644 index 0000000..d8b11cf --- /dev/null +++ b/libs/pproc/include/pproc_expand.h @@ -0,0 +1,26 @@ +#ifndef __SCC_PPROC_EXPAND_H__ +#define __SCC_PPROC_EXPAND_H__ + +#include +#include +#include +#include + +typedef struct { + scc_pproc_macro_table_t *macro_table; + scc_lexer_tok_ring_t *input; + scc_lexer_tok_vec_t output; + scc_pproc_macro_table_t expanded_set; + int need_rescan; +} scc_pproc_expand_t; + +static inline scc_lexer_tok_ring_t +scc_lexer_array_to_ring(scc_lexer_tok_vec_t *array) { + scc_lexer_tok_ring_t ret; + scc_ring_by_buffer(ret, array->data, array->size); + return ret; +} + +void scc_pproc_expand_macro(scc_pproc_expand_t *expand_ctx); + +#endif /* __SCC_PPROC_EXPAND_H__ */ diff --git a/libs/pproc/include/pproc_macro.h b/libs/pproc/include/pproc_macro.h index fe22d6d..f88ea01 100644 --- a/libs/pproc/include/pproc_macro.h +++ b/libs/pproc/include/pproc_macro.h @@ -10,21 +10,20 @@ typedef enum { SCC_PP_MACRO_NONE, // 不是宏 SCC_PP_MACRO_OBJECT, // 对象宏 SCC_PP_MACRO_FUNCTION, // 函数宏 -} scc_pp_macro_type_t; - -typedef scc_lexer_tok_vec_t scc_pproc_macro_list_t; +} scc_pproc_macro_type_t; +typedef SCC_VEC(scc_lexer_tok_vec_t) scc_pproc_macro_extened_params_t; // 宏定义结构 typedef struct scc_macro { - scc_cstring_t name; // 宏名称 - scc_pp_macro_type_t type; // 宏类型 - scc_lexer_tok_vec_t replaces; // 替换列表 - scc_pproc_macro_list_t params; // 参数列表(仅函数宏) -} scc_pp_macro_t; + scc_cstring_t name; // 宏名称 + scc_pproc_macro_type_t type; // 宏类型 + scc_lexer_tok_vec_t replaces; // 替换列表 + scc_lexer_tok_vec_t params; // 参数列表(仅函数宏) +} scc_pproc_macro_t; typedef struct scc_macro_table { scc_hashtable_t table; // 宏定义表 -} scc_pp_macro_table_t; +} scc_pproc_macro_table_t; /** * @brief 创建宏对象 @@ -32,14 +31,14 @@ typedef struct scc_macro_table { * @param type 宏类型 * @return 创建的宏对象指针,失败返回NULL */ -scc_pp_macro_t *scc_pp_macro_new(const scc_cstring_t *name, - scc_pp_macro_type_t type); +scc_pproc_macro_t *scc_pproc_macro_new(const scc_cstring_t *name, + scc_pproc_macro_type_t type); /** * @brief 销毁宏对象 * @param macro 要销毁的宏对象 */ -void scc_pp_macro_drop(scc_pp_macro_t *macro); +void scc_pproc_macro_drop(scc_pproc_macro_t *macro); /** * @brief 添加对象宏 @@ -48,9 +47,9 @@ void scc_pp_macro_drop(scc_pp_macro_t *macro); * @param replacement 替换文本列表 * @return 成功返回true,失败返回false */ -cbool scc_pp_add_object_macro(scc_pp_macro_table_t *pp, - const scc_cstring_t *name, - const scc_pproc_macro_list_t *replacement); +cbool scc_pproc_add_object_macro(scc_pproc_macro_table_t *pp, + const scc_cstring_t *name, + const scc_lexer_tok_vec_t *replacement); /** * @brief 添加函数宏 @@ -60,19 +59,19 @@ cbool scc_pp_add_object_macro(scc_pp_macro_table_t *pp, * @param replacement 替换文本列表 * @return 成功返回true,失败返回false */ -cbool scc_pp_add_function_macro(scc_pp_macro_table_t *pp, - const scc_cstring_t *name, - const scc_pproc_macro_list_t *params, - const scc_pproc_macro_list_t *replacement); +cbool scc_pproc_add_function_macro(scc_pproc_macro_table_t *pp, + const scc_cstring_t *name, + const scc_lexer_tok_vec_t *params, + const scc_lexer_tok_vec_t *replacement); /** * @brief * * @param pp * @param macro - * @return scc_pp_macro_t* + * @return scc_pproc_macro_t* */ -scc_pp_macro_t *scc_pp_macro_table_set(scc_pp_macro_table_t *pp, - scc_pp_macro_t *macro); +scc_pproc_macro_t *scc_pproc_macro_table_set(scc_pproc_macro_table_t *pp, + scc_pproc_macro_t *macro); /** * @brief 查找宏定义 @@ -80,8 +79,8 @@ scc_pp_macro_t *scc_pp_macro_table_set(scc_pp_macro_table_t *pp, * @param name 宏名称 * @return 找到的宏对象指针,未找到返回NULL */ -scc_pp_macro_t *scc_pp_macro_table_get(scc_pp_macro_table_t *pp, - const scc_cstring_t *name); +scc_pproc_macro_t *scc_pproc_macro_table_get(scc_pproc_macro_table_t *pp, + const scc_cstring_t *name); /** * @brief 从预处理器中删除宏 @@ -89,9 +88,10 @@ scc_pp_macro_t *scc_pp_macro_table_get(scc_pp_macro_table_t *pp, * @param name 宏名称 * @return 成功删除返回true,未找到返回false */ -cbool scc_pp_macro_table_remove(scc_pp_macro_table_t *pp, - const scc_cstring_t *name); +cbool scc_pproc_macro_table_remove(scc_pproc_macro_table_t *pp, + const scc_cstring_t *name); + +void scc_pproc_marco_table_init(scc_pproc_macro_table_t *macros); +void scc_pproc_macro_table_drop(scc_pproc_macro_table_t *macros); -void scc_pp_marco_table_init(scc_pp_macro_table_t *macros); -void scc_pp_macro_table_drop(scc_pp_macro_table_t *macros); #endif /* __SCC_PP_MACRO_H__ */ diff --git a/libs/pproc/include/scc_pproc.h b/libs/pproc/include/scc_pproc.h index 255d1b4..0f240e4 100644 --- a/libs/pproc/include/scc_pproc.h +++ b/libs/pproc/include/scc_pproc.h @@ -28,18 +28,18 @@ typedef struct { // 可能还需要保存当前位置等 } scc_pproc_file_state_t; typedef SCC_VEC(scc_pproc_file_state_t) scc_pproc_file_stack_t; - typedef SCC_VEC(scc_lexer_tok_ring_t *) scc_pproc_ring_vec_t; + typedef struct scc_pproc { scc_lexer_tok_ring_t *cur_ring; + scc_lexer_tok_ring_t expanded_ring; scc_strpool_t strpool; + int at_line_start; - scc_pp_macro_table_t macro_table; + scc_pproc_macro_table_t macro_table; scc_pproc_if_stack_t if_stack; scc_pproc_file_stack_t file_stack; - scc_lexer_tok_vec_t cache; - int cache_pos; scc_lexer_tok_ring_t ring; int ring_ref_count; } scc_pproc_t; @@ -49,10 +49,9 @@ scc_lexer_tok_ring_t *scc_pproc_to_ring(scc_pproc_t *pp, int ring_size); void scc_pproc_drop(scc_pproc_t *pp); void scc_pproc_handle_directive(scc_pproc_t *pp); -void scc_pproc_expand_macro(scc_pproc_t *pp, const scc_pp_macro_t *macro); - -void scc_pproc_parse_macro_arguments(scc_pproc_t *pp, - scc_pproc_macro_list_t *args); +void scc_pproc_expand_by_src(scc_pproc_t *pp, const scc_pproc_macro_t *macro); +void scc_pproc_parse_macro_arguments(scc_lexer_tok_ring_t *ring, + scc_lexer_tok_vec_t *args, int need_full); void scc_pproc_parse_function_macro(scc_pproc_t *pp, const scc_lexer_tok_t *ident); void scc_pproc_parse_object_macro(scc_pproc_t *pp, diff --git a/libs/pproc/src/pproc_directive.c b/libs/pproc/src/pproc_directive.c index 1942960..323b4d0 100644 --- a/libs/pproc/src/pproc_directive.c +++ b/libs/pproc/src/pproc_directive.c @@ -44,28 +44,34 @@ static inline int keyword_cmp(const char *name, int len) { return -1; // Not a keyword. } -void scc_pproc_parse_macro_arguments(scc_pproc_t *pp, - scc_pproc_macro_list_t *args) { - Assert(pp != null && args != null); +void scc_pproc_parse_macro_arguments(scc_lexer_tok_ring_t *ring, + scc_lexer_tok_vec_t *args, int need_full) { + Assert(ring != null && args != null); scc_lexer_tok_t tok = {0}; - scc_vec_init(*args); int depth = 0; + int ok; do { - scc_lexer_next_non_blank(pp->cur_ring, &tok); - if (tok.type == SCC_TOK_L_PAREN) { - depth++; - } else if (tok.type == SCC_TOK_R_PAREN) { + scc_ring_next_consume(*ring, tok, ok); + if (!ok) { + return; + } + // scc_lexer_next_non_blank(ring, &tok); + if (tok.type == SCC_TOK_R_PAREN) { depth--; } - if (depth > 1) { + if (depth > 0 || need_full) { scc_vec_push(*args, tok); } else { scc_lexer_tok_drop(&tok); } + if (tok.type == SCC_TOK_L_PAREN) { + depth++; + } } while (depth); } -static inline void fill_replacements(scc_pproc_t *pp, scc_pp_macro_t *macro) { +static inline void fill_replacements(scc_pproc_t *pp, + scc_pproc_macro_t *macro) { int ok; scc_lexer_tok_t tok; ok = scc_lexer_next_non_blank(pp->cur_ring, &tok); @@ -88,50 +94,56 @@ static inline void fill_replacements(scc_pproc_t *pp, scc_pp_macro_t *macro) { void scc_pproc_parse_function_macro(scc_pproc_t *pp, const scc_lexer_tok_t *ident) { - scc_pproc_macro_list_t args; - scc_pproc_parse_macro_arguments(pp, &args); - scc_pp_macro_t *macro = - scc_pp_macro_new(&ident->lexeme, SCC_PP_MACRO_FUNCTION); + scc_lexer_tok_vec_t args; + scc_vec_init(args); + scc_pproc_parse_macro_arguments(pp->cur_ring, &args, false); + scc_pproc_macro_t *macro = + scc_pproc_macro_new(&ident->lexeme, SCC_PP_MACRO_FUNCTION); /* check and set params 1. identifier-list(opt) 2. ... 3. identifier-list , ... */ + int idx = 0; scc_vec_foreach(args, i) { scc_lexer_tok_t *arg = &scc_vec_at(args, i); if (arg->type == SCC_TOK_COMMA) { scc_lexer_tok_drop(arg); - if (i % 2 != 1) { + if (idx++ % 2 != 1) { LOG_FATAL("ERROR"); } } else if (arg->type == SCC_TOK_IDENT) { - if (i % 2 != 0) { + if (idx++ % 2 != 0) { LOG_FATAL("ERROR"); } scc_vec_push(macro->params, *arg); } else if (arg->type == SCC_TOK_ELLIPSIS) { - if (i % 2 != 0) { + if (idx++ % 2 != 0) { LOG_FATAL("ERROR"); } scc_cstring_t va_args = scc_cstring_from_cstr("__VA_ARGS__"); scc_cstring_free(&arg->lexeme); arg->lexeme = va_args; scc_vec_push(macro->params, *arg); + } else if (scc_get_tok_subtype(arg->type) == + SCC_TOK_SUBTYPE_EMPTYSPACE || + scc_get_tok_subtype(arg->type) == SCC_TOK_SUBTYPE_COMMENT) { + scc_lexer_tok_drop(arg); } else { LOG_FATAL("ERROR"); } } fill_replacements(pp, macro); - scc_pp_macro_table_set(&pp->macro_table, macro); + scc_pproc_macro_table_set(&pp->macro_table, macro); } void scc_pproc_parse_object_macro(scc_pproc_t *pp, const scc_lexer_tok_t *ident) { - scc_pp_macro_t *macro = - scc_pp_macro_new(&ident->lexeme, SCC_PP_MACRO_OBJECT); + scc_pproc_macro_t *macro = + scc_pproc_macro_new(&ident->lexeme, SCC_PP_MACRO_OBJECT); fill_replacements(pp, macro); - scc_pp_macro_table_set(&pp->macro_table, macro); + scc_pproc_macro_table_set(&pp->macro_table, macro); } /* @@ -245,7 +257,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) { LOG_ERROR("expected identifier"); goto ERROR; } - scc_pp_macro_table_remove(&pp->macro_table, &tok.lexeme); + scc_pproc_macro_table_remove(&pp->macro_table, &tok.lexeme); scc_lexer_tok_drop(&tok); scc_lexer_next_non_blank(pp->cur_ring, &tok); if (tok.type != SCC_TOK_ENDLINE) { @@ -254,7 +266,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) { goto ERROR; } scc_lexer_tok_drop(&tok); - break; + return; } case SCC_PP_TOK_INCLUDE: case SCC_PP_TOK_IF: diff --git a/libs/pproc/src/pproc_expand.c b/libs/pproc/src/pproc_expand.c index 8538ed2..6917a52 100644 --- a/libs/pproc/src/pproc_expand.c +++ b/libs/pproc/src/pproc_expand.c @@ -1,32 +1,263 @@ +#include #include -typedef struct { +static scc_lexer_tok_t stringify_argument(scc_lexer_tok_vec_t *arg_tokens) { + scc_cstring_t str = scc_cstring_create(); + scc_cstring_append_ch(&str, '\"'); // 左引号 -} scc_expand_t; - -void scc_pproc_expand_macro(scc_pproc_t *pp, const scc_pp_macro_t *macro) { - if (macro->type == SCC_PP_MACRO_NONE) { - UNREACHABLE(); - } - - if (macro->type == SCC_PP_MACRO_OBJECT) { - scc_vec_foreach(macro->replaces, i) { - scc_lexer_tok_t tok = scc_vec_at(macro->replaces, i); - if (tok.type == SCC_TOK_BLANK) { - tok.lexeme = scc_cstring_from_cstr(" "); - } else { - tok.lexeme = scc_cstring_copy(&tok.lexeme); - } - scc_vec_push(pp->cache, tok); + int need_space = 0; // 是否需要插入空格 + for (usize i = 0; i < arg_tokens->size; i++) { + scc_lexer_tok_t *tok = &scc_vec_at(*arg_tokens, i); + if (tok->type == SCC_TOK_BLANK) { + need_space = 1; // 标记遇到空白 + continue; } - pp->cache_pos = 0; - return; + + // 需要空格且当前不是第一个有效token,插入一个空格 + if (need_space && i > 0) { + scc_cstring_append_ch(&str, ' '); + need_space = 0; + } + + // 对字符串/字符常量内的 " 和 \ 进行转义 + if (tok->type == SCC_TOK_STRING_LITERAL || + tok->type == SCC_TOK_CHAR_LITERAL) { + // 注意:lex包含两端的引号,需要跳过首尾,转义内部字符 + // 简化:暂不处理内部转义,直接追加 + } + scc_cstring_append(&str, &tok->lexeme); + need_space = 0; + } + scc_cstring_append_ch(&str, '\"'); // 右引号 + + scc_lexer_tok_t result; + result.type = SCC_TOK_STRING_LITERAL; + result.lexeme = str; + return result; +} + +static inline void scc_copy_expand(scc_pproc_expand_t *expand_ctx, + scc_pproc_expand_t *copyed_ctx, + scc_lexer_tok_ring_t *ring) { + + copyed_ctx->input = ring; + copyed_ctx->expanded_set = expand_ctx->expanded_set; + copyed_ctx->macro_table = expand_ctx->macro_table; + copyed_ctx->need_rescan = false; + scc_vec_init(copyed_ctx->output); +} + +void scc_pproc_expand_by_src(scc_pproc_t *pp, const scc_pproc_macro_t *macro) { + scc_pproc_expand_t ctx; + scc_lexer_tok_vec_t expaned_buffer; + scc_vec_init(expaned_buffer); + + int ok; + scc_lexer_tok_t tok; + scc_ring_next_consume(*pp->cur_ring, tok, ok); + if (macro->type == SCC_PP_MACRO_NONE || ok == false) { + UNREACHABLE(); + } else if (macro->type == SCC_PP_MACRO_OBJECT) { + scc_vec_push(expaned_buffer, tok); + } else if (macro->type == SCC_PP_MACRO_FUNCTION) { + scc_vec_push(expaned_buffer, tok); + scc_pproc_parse_macro_arguments(pp->cur_ring, &expaned_buffer, true); + } + + scc_lexer_tok_ring_t ring = scc_lexer_array_to_ring(&expaned_buffer); + ctx.input = ˚ + ctx.macro_table = &pp->macro_table; + ctx.need_rescan = false; + scc_vec_init(ctx.output); + + scc_pproc_marco_table_init(&ctx.expanded_set); + scc_pproc_expand_macro(&ctx); + pp->expanded_ring = scc_lexer_array_to_ring(&ctx.output); + scc_pproc_macro_table_drop(&ctx.expanded_set); +} + +static inline void +split_arguments(scc_pproc_macro_extened_params_t *splited_params, + scc_lexer_tok_vec_t *raw_args) { + scc_lexer_tok_vec_t arg; + scc_vec_init(arg); + scc_vec_foreach(*raw_args, i) { + scc_lexer_tok_t *raw_arg = &scc_vec_at(*raw_args, i); + if (raw_arg->type == SCC_TOK_COMMA) { + scc_lexer_tok_drop(raw_arg); + scc_vec_push(*splited_params, arg); + scc_vec_init(arg); + } else { + if (raw_arg->type == SCC_TOK_BLANK || + scc_get_tok_subtype(raw_arg->type) == + SCC_TOK_SUBTYPE_EMPTYSPACE) { + scc_lexer_tok_drop(raw_arg); + } + scc_vec_push(arg, *raw_arg); + } + } + scc_vec_push(*splited_params, arg); +} + +static inline void +expand_arguments(scc_pproc_macro_extened_params_t *expanded_params, + scc_pproc_macro_extened_params_t *splited_params, + scc_pproc_expand_t *expand_ctx) { + scc_vec_foreach(*splited_params, i) { + scc_pproc_expand_t ctx; + scc_lexer_tok_vec_t splite_param = scc_vec_at(*splited_params, i); + scc_lexer_tok_vec_t expanded_param; + scc_vec_init(expanded_param); + scc_vec_foreach(splite_param, j) { + scc_lexer_tok_t tok = scc_vec_at(splite_param, j); + tok.lexeme = scc_cstring_copy(&tok.lexeme); + scc_vec_push(expanded_param, tok); + } + scc_lexer_tok_ring_t ring = scc_lexer_array_to_ring(&expanded_param); + scc_copy_expand(expand_ctx, &ctx, &ring); + scc_pproc_expand_macro(&ctx); + scc_vec_push(*expanded_params, ctx.output); + } +} + +static inline void +expanded_params_free(scc_pproc_macro_extened_params_t *expanded_params) { + scc_vec_foreach(*expanded_params, i) { + scc_lexer_tok_vec_t expanded_param = scc_vec_at(*expanded_params, i); + scc_vec_foreach(expanded_param, j) { + scc_lexer_tok_t tok = scc_vec_at(expanded_param, j); + scc_lexer_tok_drop(&tok); + } + scc_vec_free(expanded_param); + } + scc_vec_free(*expanded_params); +} + +static inline void expand_function_macro(scc_pproc_expand_t *expand_ctx, + const scc_pproc_macro_t *macro) { + Assert(macro->type == SCC_PP_MACRO_FUNCTION); + scc_lexer_tok_vec_t raw_args; + scc_vec_init(raw_args); + scc_pproc_parse_macro_arguments(expand_ctx->input, &raw_args, false); + + // collect, fill and expand arg + scc_pproc_macro_extened_params_t splited_params; + scc_vec_init(splited_params); + split_arguments(&splited_params, &raw_args); + + scc_pproc_macro_extened_params_t expanded_params; + scc_vec_init(expanded_params); + expand_arguments(&expanded_params, &splited_params, expand_ctx); + + // replace + scc_vec_foreach(macro->replaces, i) { + scc_lexer_tok_t tok = scc_vec_at(macro->replaces, i); + scc_lexer_tok_t prev_tok = {0}; + if (i >= 1) { + prev_tok = scc_vec_at(macro->replaces, i - 1); + } + if (tok.type == SCC_TOK_BLANK) { + tok.lexeme = scc_cstring_from_cstr(" "); + scc_vec_push(expand_ctx->output, tok); + continue; + } + scc_vec_foreach(macro->params, j) { + if (scc_cstring_cmp(&tok.lexeme, + &(scc_vec_at(macro->params, j).lexeme)) == 0) { + if (j >= scc_vec_size(expanded_params)) { + LOG_ERROR("Invalid macro parameter"); + goto CONTINUE; + } + + if (prev_tok.type == SCC_TOK_SHARP) { + // # stringify + scc_lexer_tok_t out = + stringify_argument(&scc_vec_at(splited_params, j)); + scc_vec_pop(expand_ctx->output); + scc_vec_push(expand_ctx->output, out); + } else { + scc_lexer_tok_vec_t expanded_param = + scc_vec_at(expanded_params, j); + scc_vec_foreach(expanded_param, k) { + tok = scc_vec_at(expanded_param, k); + tok.lexeme = scc_cstring_copy(&tok.lexeme); + scc_vec_push(expand_ctx->output, tok); + } + } + goto CONTINUE; + } + } + tok.lexeme = scc_cstring_copy(&tok.lexeme); + scc_vec_push(expand_ctx->output, tok); + CONTINUE: + continue; + } + expanded_params_free(&splited_params); + expanded_params_free(&expanded_params); +} + +static inline void expand_object_macro(scc_pproc_expand_t *expand_ctx, + const scc_pproc_macro_t *macro) { + scc_vec_foreach(macro->replaces, i) { + scc_lexer_tok_t tok = scc_vec_at(macro->replaces, i); + if (tok.type == SCC_TOK_BLANK) { + tok.lexeme = scc_cstring_from_cstr(" "); + } else { + tok.lexeme = scc_cstring_copy(&tok.lexeme); + } + scc_vec_push(expand_ctx->output, tok); + } +} + +void scc_pproc_expand_macro(scc_pproc_expand_t *expand_ctx) { + int ok; + scc_lexer_tok_t tok; + while (1) { + scc_ring_next_consume(*expand_ctx->input, tok, ok); + if (!ok) { + return; + } + if (tok.type != SCC_TOK_IDENT) { + scc_vec_push(expand_ctx->output, tok); + continue; + } + // maybe expanded + scc_pproc_macro_t *macro = + scc_pproc_macro_table_get(expand_ctx->macro_table, &tok.lexeme); + + if (macro == null || scc_pproc_macro_table_get( + &expand_ctx->expanded_set, ¯o->name)) { + scc_vec_push(expand_ctx->output, tok); + continue; + } + expand_ctx->need_rescan = true; + + scc_pproc_macro_t *expanded_macro = + scc_pproc_macro_new(¯o->name, macro->type); + if (expanded_macro == null) { + LOG_FATAL("Out of memory"); + } + scc_pproc_macro_table_set(&expand_ctx->expanded_set, expanded_macro); + if (macro->type == SCC_PP_MACRO_OBJECT) { + expand_object_macro(expand_ctx, macro); + } else if (macro->type == SCC_PP_MACRO_FUNCTION) { + expand_function_macro(expand_ctx, macro); + } else { + UNREACHABLE(); + } + RESCAN: + scc_pproc_macro_table_remove(&expand_ctx->expanded_set, ¯o->name); + // TODO expand # and ## + continue; + } + if (expand_ctx->need_rescan) { + expand_ctx->need_rescan = false; + scc_pproc_expand_t rescan_ctx; + scc_lexer_tok_ring_t ring = + scc_lexer_array_to_ring(&expand_ctx->output); + scc_copy_expand(expand_ctx, &rescan_ctx, &ring); + scc_pproc_expand_macro(&rescan_ctx); + scc_ring_free(ring); + expand_ctx->output = rescan_ctx.output; } - Assert(macro->type == SCC_PP_MACRO_FUNCTION); - // Check params match - scc_pproc_macro_list_t args; - scc_pproc_parse_macro_arguments(pp, &args); - scc_vec_foreach(args, i) {} - scc_vec_foreach(macro->params, i) {} - scc_vec_foreach(macro->replaces, i) {} } diff --git a/libs/pproc/src/pproc_macro.c b/libs/pproc/src/pproc_macro.c index 813d2ad..7a49a85 100644 --- a/libs/pproc/src/pproc_macro.c +++ b/libs/pproc/src/pproc_macro.c @@ -1,9 +1,9 @@ #include // 创建宏对象 -scc_pp_macro_t *scc_pp_macro_new(const scc_cstring_t *name, - scc_pp_macro_type_t type) { - scc_pp_macro_t *macro = scc_malloc(sizeof(scc_pp_macro_t)); +scc_pproc_macro_t *scc_pproc_macro_new(const scc_cstring_t *name, + scc_pproc_macro_type_t type) { + scc_pproc_macro_t *macro = scc_malloc(sizeof(scc_pproc_macro_t)); if (!macro) { LOG_ERROR("Failed to allocate memory for macro"); return null; @@ -18,7 +18,7 @@ scc_pp_macro_t *scc_pp_macro_new(const scc_cstring_t *name, } // 销毁宏对象 -void scc_pp_macro_drop(scc_pp_macro_t *macro) { +void scc_pproc_macro_drop(scc_pproc_macro_t *macro) { if (!macro) return; @@ -40,23 +40,24 @@ void scc_pp_macro_drop(scc_pp_macro_t *macro) { } // 添加对象宏 -cbool scc_pp_add_object_macro(scc_pp_macro_table_t *macros, - const scc_cstring_t *name, - const scc_pproc_macro_list_t *replacement) { +cbool scc_pproc_add_object_macro(scc_pproc_macro_table_t *macros, + const scc_cstring_t *name, + const scc_lexer_tok_vec_t *replacement) { if (!macros || !name || !replacement) return false; - scc_pp_macro_t *macro = scc_pp_macro_new(name, SCC_PP_MACRO_OBJECT); + scc_pproc_macro_t *macro = scc_pproc_macro_new(name, SCC_PP_MACRO_OBJECT); if (!macro) return false; macro->replaces = *replacement; // 检查是否已存在同名宏 - scc_pp_macro_t *existing = scc_hashtable_get(¯os->table, ¯o->name); + scc_pproc_macro_t *existing = + scc_hashtable_get(¯os->table, ¯o->name); if (existing) { LOG_WARN("Redefining macro: %s", scc_cstring_as_cstr(¯o->name)); - scc_pp_macro_drop(existing); + scc_pproc_macro_drop(existing); } scc_hashtable_set(¯os->table, ¯o->name, macro); @@ -64,14 +65,14 @@ cbool scc_pp_add_object_macro(scc_pp_macro_table_t *macros, } // 添加函数宏 -cbool scc_pp_add_function_macro(scc_pp_macro_table_t *macros, - const scc_cstring_t *name, - const scc_pproc_macro_list_t *params, - const scc_pproc_macro_list_t *replacement) { +cbool scc_pproc_add_function_macro(scc_pproc_macro_table_t *macros, + const scc_cstring_t *name, + const scc_lexer_tok_vec_t *params, + const scc_lexer_tok_vec_t *replacement) { if (!macros || !name || !params || !replacement) return false; - scc_pp_macro_t *macro = scc_pp_macro_new(name, SCC_PP_MACRO_FUNCTION); + scc_pproc_macro_t *macro = scc_pproc_macro_new(name, SCC_PP_MACRO_FUNCTION); if (!macro) return false; @@ -80,10 +81,11 @@ cbool scc_pp_add_function_macro(scc_pp_macro_table_t *macros, macro->replaces = *replacement; // 检查是否已存在同名宏 - scc_pp_macro_t *existing = scc_hashtable_get(¯os->table, ¯o->name); + scc_pproc_macro_t *existing = + scc_hashtable_get(¯os->table, ¯o->name); if (existing) { LOG_WARN("Redefining macro: %s", scc_cstring_as_cstr(¯o->name)); - scc_pp_macro_drop(existing); + scc_pproc_macro_drop(existing); } scc_hashtable_set(¯os->table, ¯o->name, macro); @@ -92,30 +94,30 @@ cbool scc_pp_add_function_macro(scc_pp_macro_table_t *macros, /// marco_table -scc_pp_macro_t *scc_pp_macro_table_set(scc_pp_macro_table_t *pp, - scc_pp_macro_t *macro) { +scc_pproc_macro_t *scc_pproc_macro_table_set(scc_pproc_macro_table_t *pp, + scc_pproc_macro_t *macro) { Assert(pp != null && macro != null); return scc_hashtable_set(&pp->table, ¯o->name, macro); } // 查找宏定义 -scc_pp_macro_t *scc_pp_macro_table_get(scc_pp_macro_table_t *pp, - const scc_cstring_t *name) { +scc_pproc_macro_t *scc_pproc_macro_table_get(scc_pproc_macro_table_t *pp, + const scc_cstring_t *name) { return scc_hashtable_get(&pp->table, name); } // 从预处理器中删除宏 -cbool scc_pp_macro_table_remove(scc_pp_macro_table_t *pp, - const scc_cstring_t *name) { +cbool scc_pproc_macro_table_remove(scc_pproc_macro_table_t *pp, + const scc_cstring_t *name) { if (!pp || !name) return false; - scc_pp_macro_t *macro = scc_hashtable_get(&pp->table, name); + scc_pproc_macro_t *macro = scc_hashtable_get(&pp->table, name); if (!macro) return false; scc_hashtable_del(&pp->table, name); - scc_pp_macro_drop(macro); + scc_pproc_macro_drop(macro); return true; } @@ -134,7 +136,7 @@ static int hash_cmp(const void *key1, const void *key2) { return scc_strcmp(scc_cstring_as_cstr(str1), scc_cstring_as_cstr(str2)); } -void scc_pp_marco_table_init(scc_pp_macro_table_t *macros) { +void scc_pproc_marco_table_init(scc_pproc_macro_table_t *macros) { Assert(macros != null); macros->table.hash_func = hash_func; macros->table.key_cmp = hash_cmp; @@ -144,11 +146,11 @@ void scc_pp_marco_table_init(scc_pp_macro_table_t *macros) { static int macro_free(const void *key, void *value, void *context) { (void)key; (void)context; - scc_pp_macro_drop(value); + scc_pproc_macro_drop(value); return 0; } -void scc_pp_macro_table_drop(scc_pp_macro_table_t *macros) { +void scc_pproc_macro_table_drop(scc_pproc_macro_table_t *macros) { Assert(macros != null); scc_hashtable_foreach(¯os->table, macro_free, null); scc_hashtable_drop(¯os->table); diff --git a/libs/pproc/src/scc_pproc.c b/libs/pproc/src/scc_pproc.c index 5faf85e..af60a0a 100644 --- a/libs/pproc/src/scc_pproc.c +++ b/libs/pproc/src/scc_pproc.c @@ -5,30 +5,44 @@ static int pproc_next(scc_pproc_t *pp, scc_lexer_tok_t *out) { scc_lexer_tok_t tok = {0}; int ok = 0; CONTINUE: - if (scc_vec_size(pp->cache)) { - // use cache? - *out = scc_vec_at(pp->cache, pp->cache_pos); - pp->cache_pos++; - if (pp->cache_pos == scc_vec_size(pp->cache)) { - pp->cache_pos = 0; - scc_vec_free(pp->cache); + if (pp->expanded_ring.cap) { + scc_ring_next_consume(pp->expanded_ring, *out, ok); + if (ok == false) { + scc_ring_free(pp->expanded_ring); + goto CONTINUE; + } else { + return true; } - return true; } scc_ring_peek(*stream, tok, ok); - if (tok.type == SCC_TOK_SHARP && tok.loc.col == 1) { + if (tok.type == SCC_TOK_BLANK || + scc_get_tok_subtype(tok.type) == SCC_TOK_SUBTYPE_COMMENT) { + scc_cstring_free(&tok.lexeme); + scc_ring_next_consume(*stream, *out, ok); + out->lexeme = scc_cstring_from_cstr(" "); + return true; + } else if (tok.type == SCC_TOK_ENDLINE) { + scc_ring_next_consume(*stream, *out, ok); + pp->at_line_start = true; + return true; + } + + if (pp->at_line_start && tok.type == SCC_TOK_SHARP) { // parse to # scc_pproc_handle_directive(pp); goto CONTINUE; - } else if (tok.type == SCC_TOK_IDENT) { + } + + pp->at_line_start = false; + if (tok.type == SCC_TOK_IDENT) { // maybe expanded - scc_pp_macro_t *macro = - scc_pp_macro_table_get(&pp->macro_table, &tok.lexeme); - scc_ring_next_consume(*stream, *out, ok); + scc_pproc_macro_t *macro = + scc_pproc_macro_table_get(&pp->macro_table, &tok.lexeme); if (macro == null) { + scc_ring_next_consume(*stream, *out, ok); return ok; } - scc_pproc_expand_macro(pp, macro); + scc_pproc_expand_by_src(pp, macro); goto CONTINUE; } else { // continue @@ -41,11 +55,11 @@ CONTINUE: void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input) { Assert(pp != null && input != null); pp->cur_ring = input; - scc_pp_marco_table_init(&pp->macro_table); + scc_ring_init(pp->expanded_ring, 0, 0, 0); + scc_pproc_marco_table_init(&pp->macro_table); scc_vec_init(pp->if_stack); scc_vec_init(pp->file_stack); - scc_vec_init(pp->cache); - pp->cache_pos = 0; + pp->at_line_start = true; } static cbool fill_token(scc_lexer_tok_t *tok, void *userdata) { @@ -64,5 +78,5 @@ void scc_pproc_drop(scc_pproc_t *pp) { if (pp == null) return; scc_lexer_drop_ring(pp->cur_ring); - scc_pp_macro_table_drop(&pp->macro_table); + scc_pproc_macro_table_drop(&pp->macro_table); } diff --git a/libs/pproc/tests/test_unit.c b/libs/pproc/tests/test_unit.c index 7c3b14e..a7fb9c5 100644 --- a/libs/pproc/tests/test_unit.c +++ b/libs/pproc/tests/test_unit.c @@ -88,6 +88,7 @@ static void test_define_object_macro_backspace(void) { static void test_define_function_macro(void) { TEST_CASE("function-like macro"); CHECK_PP_OUTPUT_EXACT("#define ADD(a,b) a + b\nADD(1, 2)\n", "1 + 2\n"); + CHECK_PP_OUTPUT_EXACT("#define ADD( a , b ) a + b\nADD(1, 2)\n", "1 + 2\n"); CHECK_PP_OUTPUT_EXACT( "#define MAX(a,b) ((a) > (b) ? (a) : (b))\nMAX(10, 20)\n", "((10) > (20) ? (10) : (20))\n"); @@ -99,6 +100,8 @@ static void test_define_stringify_operator(void) { "\"hello\"\n"); CHECK_PP_OUTPUT_EXACT("#define STR(x) #x\nSTR(test value)\n", "\"test value\"\n"); + CHECK_PP_OUTPUT_EXACT("#define STR(x) #x\nSTR(A B \"ab\")\n", + "\"A B \"ab\"\"\n"); } static void test_define_concat_operator(void) { @@ -206,7 +209,7 @@ static void test_error_cases(void) { static void test_edge_cases(void) { TEST_CASE("empty macro"); - CHECK_PP_OUTPUT_EXACT("#define EMPTY\nEMPTY\n", "\n"); + CHECK_PP_OUTPUT_EXACT("#define EMPTY()\nEMPTY()\n", "\n"); TEST_CASE("macro with only spaces"); CHECK_PP_OUTPUT_EXACT("#define SPACE \nSPACE\n", "\n");