Compare commits

...

2 Commits

Author SHA1 Message Date
zzy
c86071416d feat(pproc): 实现宏展开功能并重构宏定义接口
- 新增 pproc_expand.h 头文件,定义宏展开相关的数据结构和函数接口
- 重命名宏相关类型和函数,将 scc_pp_* 前缀统一改为 scc_pproc_*
- 修改宏参数解析逻辑,支持更灵活的参数处理方式
- 实现完整的宏展开功能,包括对象宏和函数宏的展开
- 添加字符串化操作符 (#) 的支持
- 改进预处理器主循环逻辑,优化宏展开流程
- 更新单元测试用例,增加对宏参数解析和字符串化的测试
2026-02-18 18:18:57 +08:00
zzy
9d85dc130d feat(lexer): 添加词法分析器对##操作符的支持
- 重命名lexer_token.h为scc_lexer_token.h以保持命名一致性
- 在词法分析器中实现##操作符的识别和处理
- 修改头文件包含路径和类型定义的位置
- 修复token结构体定义的顺序问题

fix(lexer): 初始化lexer中的cur变量避免未初始化问题

- 在scc_lexer_get_token函数中初始化scc_sstream_char_t cur变量

refactor(core): 增强ring缓冲区功能并添加cstring比较函数

- 在scc_core_ring.h中添加空值检查防止fill函数为空时崩溃
- 添加scc_ring_by_buffer宏用于通过缓冲区创建ring实例
- 在scc_core_str.h中添加scc_cstring_cmp函数用于字符串比较
2026-02-18 18:17:52 +08:00
14 changed files with 457 additions and 144 deletions

View File

@@ -6,13 +6,11 @@
#ifndef __SCC_LEXER_H__ #ifndef __SCC_LEXER_H__
#define __SCC_LEXER_H__ #define __SCC_LEXER_H__
#include "lexer_token.h" #include "scc_lexer_token.h"
#include <scc_core.h> #include <scc_core.h>
#include <scc_core_ring.h> #include <scc_core_ring.h>
#include <scc_sstream.h> #include <scc_sstream.h>
typedef SCC_RING(scc_lexer_tok_t) scc_lexer_tok_ring_t;
typedef SCC_VEC(scc_lexer_tok_t) scc_lexer_tok_vec_t;
/** /**
* @brief 词法分析器核心结构体 * @brief 词法分析器核心结构体
* *

View File

@@ -4,6 +4,12 @@
#include <scc_core.h> #include <scc_core.h>
#include <scc_pos.h> #include <scc_pos.h>
#include <scc_core_ring.h>
struct scc_lexer_token;
typedef struct scc_lexer_token scc_lexer_tok_t;
typedef SCC_RING(scc_lexer_tok_t) scc_lexer_tok_ring_t;
typedef SCC_VEC(scc_lexer_tok_t) scc_lexer_tok_vec_t;
typedef enum scc_cstd { typedef enum scc_cstd {
SCC_CSTD_C89, SCC_CSTD_C89,
SCC_CSTD_C99, SCC_CSTD_C99,
@@ -82,6 +88,7 @@ typedef enum scc_cstd {
X(blank , SCC_TOK_SUBTYPE_EMPTYSPACE, SCC_TOK_BLANK ) \ X(blank , SCC_TOK_SUBTYPE_EMPTYSPACE, SCC_TOK_BLANK ) \
X(endline , SCC_TOK_SUBTYPE_EMPTYSPACE, SCC_TOK_ENDLINE ) \ X(endline , SCC_TOK_SUBTYPE_EMPTYSPACE, SCC_TOK_ENDLINE ) \
X("#" , SCC_TOK_SUBTYPE_OPERATOR, SCC_TOK_SHARP ) \ X("#" , SCC_TOK_SUBTYPE_OPERATOR, SCC_TOK_SHARP ) \
X("##" , SCC_TOK_SUBTYPE_OPERATOR, SCC_TOK_SHARP_SHARP ) \
X("==" , SCC_TOK_SUBTYPE_OPERATOR, SCC_TOK_EQ ) \ X("==" , SCC_TOK_SUBTYPE_OPERATOR, SCC_TOK_EQ ) \
X("=" , SCC_TOK_SUBTYPE_OPERATOR, SCC_TOK_ASSIGN ) \ X("=" , SCC_TOK_SUBTYPE_OPERATOR, SCC_TOK_ASSIGN ) \
X("++" , SCC_TOK_SUBTYPE_OPERATOR, SCC_TOK_ADD_ADD ) \ X("++" , SCC_TOK_SUBTYPE_OPERATOR, SCC_TOK_ADD_ADD ) \
@@ -166,18 +173,18 @@ typedef enum scc_tok_subtype {
SCC_TOK_SUBTYPE_EOF // 结束标记 SCC_TOK_SUBTYPE_EOF // 结束标记
} scc_tok_subtype_t; } scc_tok_subtype_t;
scc_tok_subtype_t scc_get_tok_subtype(scc_tok_type_t type);
const char *scc_get_tok_name(scc_tok_type_t type);
/** /**
* @brief * @brief
* @warning lexeme否则会出现内存泄漏 * @warning lexeme否则会出现内存泄漏
*/ */
typedef struct scc_lexer_token { struct scc_lexer_token {
scc_tok_type_t type; scc_tok_type_t type;
scc_cstring_t lexeme; scc_cstring_t lexeme;
scc_pos_t loc; scc_pos_t loc;
} scc_lexer_tok_t; };
scc_tok_subtype_t scc_get_tok_subtype(scc_tok_type_t type);
const char *scc_get_tok_name(scc_tok_type_t type);
static inline void scc_lexer_tok_drop(scc_lexer_tok_t *tok) { static inline void scc_lexer_tok_drop(scc_lexer_tok_t *tok) {
scc_cstring_free(&tok->lexeme); scc_cstring_free(&tok->lexeme);

View File

@@ -88,7 +88,7 @@ static inline cbool next_char(scc_lexer_t *lexer, scc_cstring_t *lexeme,
#define set_err_token(token) ((token)->type = SCC_TOK_UNKNOWN) #define set_err_token(token) ((token)->type = SCC_TOK_UNKNOWN)
void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) { void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) {
scc_sstream_char_t cur; scc_sstream_char_t cur = {0};
scc_cstring_t lex = scc_cstring_create(); // 临时lexeme scc_cstring_t lex = scc_cstring_create(); // 临时lexeme
// 尝试预览第一个字符 // 尝试预览第一个字符
@@ -439,7 +439,11 @@ void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) {
token->type = SCC_TOK_COND; token->type = SCC_TOK_COND;
break; break;
case '#': case '#':
token->type = SCC_TOK_SHARP; if (next.character == '#') {
token->type = SCC_TOK_SHARP_SHARP;
next_char(lexer, &lex, &cur);
} else
token->type = SCC_TOK_SHARP;
break; break;
default: default:
token->type = SCC_TOK_UNKNOWN; token->type = SCC_TOK_UNKNOWN;

View File

@@ -1,4 +1,4 @@
#include <lexer_token.h> #include <scc_lexer_token.h>
// 生成字符串映射(根据需求选择#str或#name // 生成字符串映射(根据需求选择#str或#name
static const char *token_strings[] = { static const char *token_strings[] = {

View File

@@ -0,0 +1,26 @@
#ifndef __SCC_PPROC_EXPAND_H__
#define __SCC_PPROC_EXPAND_H__
#include <pproc_macro.h>
#include <scc_core.h>
#include <scc_core_ring.h>
#include <scc_lexer.h>
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__ */

View File

@@ -10,21 +10,20 @@ typedef enum {
SCC_PP_MACRO_NONE, // 不是宏 SCC_PP_MACRO_NONE, // 不是宏
SCC_PP_MACRO_OBJECT, // 对象宏 SCC_PP_MACRO_OBJECT, // 对象宏
SCC_PP_MACRO_FUNCTION, // 函数宏 SCC_PP_MACRO_FUNCTION, // 函数宏
} scc_pp_macro_type_t; } scc_pproc_macro_type_t;
typedef scc_lexer_tok_vec_t scc_pproc_macro_list_t;
typedef SCC_VEC(scc_lexer_tok_vec_t) scc_pproc_macro_extened_params_t;
// 宏定义结构 // 宏定义结构
typedef struct scc_macro { typedef struct scc_macro {
scc_cstring_t name; // 宏名称 scc_cstring_t name; // 宏名称
scc_pp_macro_type_t type; // 宏类型 scc_pproc_macro_type_t type; // 宏类型
scc_lexer_tok_vec_t replaces; // 替换列表 scc_lexer_tok_vec_t replaces; // 替换列表
scc_pproc_macro_list_t params; // 参数列表(仅函数宏) scc_lexer_tok_vec_t params; // 参数列表(仅函数宏)
} scc_pp_macro_t; } scc_pproc_macro_t;
typedef struct scc_macro_table { typedef struct scc_macro_table {
scc_hashtable_t table; // 宏定义表 scc_hashtable_t table; // 宏定义表
} scc_pp_macro_table_t; } scc_pproc_macro_table_t;
/** /**
* @brief 创建宏对象 * @brief 创建宏对象
@@ -32,14 +31,14 @@ typedef struct scc_macro_table {
* @param type 宏类型 * @param type 宏类型
* @return 创建的宏对象指针失败返回NULL * @return 创建的宏对象指针失败返回NULL
*/ */
scc_pp_macro_t *scc_pp_macro_new(const scc_cstring_t *name, scc_pproc_macro_t *scc_pproc_macro_new(const scc_cstring_t *name,
scc_pp_macro_type_t type); scc_pproc_macro_type_t type);
/** /**
* @brief 销毁宏对象 * @brief 销毁宏对象
* @param macro 要销毁的宏对象 * @param macro 要销毁的宏对象
*/ */
void scc_pp_macro_drop(scc_pp_macro_t *macro); void scc_pproc_macro_drop(scc_pproc_macro_t *macro);
/** /**
* @brief 添加对象宏 * @brief 添加对象宏
@@ -48,9 +47,9 @@ void scc_pp_macro_drop(scc_pp_macro_t *macro);
* @param replacement 替换文本列表 * @param replacement 替换文本列表
* @return 成功返回true失败返回false * @return 成功返回true失败返回false
*/ */
cbool scc_pp_add_object_macro(scc_pp_macro_table_t *pp, cbool scc_pproc_add_object_macro(scc_pproc_macro_table_t *pp,
const scc_cstring_t *name, const scc_cstring_t *name,
const scc_pproc_macro_list_t *replacement); const scc_lexer_tok_vec_t *replacement);
/** /**
* @brief 添加函数宏 * @brief 添加函数宏
@@ -60,19 +59,19 @@ cbool scc_pp_add_object_macro(scc_pp_macro_table_t *pp,
* @param replacement 替换文本列表 * @param replacement 替换文本列表
* @return 成功返回true失败返回false * @return 成功返回true失败返回false
*/ */
cbool scc_pp_add_function_macro(scc_pp_macro_table_t *pp, cbool scc_pproc_add_function_macro(scc_pproc_macro_table_t *pp,
const scc_cstring_t *name, const scc_cstring_t *name,
const scc_pproc_macro_list_t *params, const scc_lexer_tok_vec_t *params,
const scc_pproc_macro_list_t *replacement); const scc_lexer_tok_vec_t *replacement);
/** /**
* @brief * @brief
* *
* @param pp * @param pp
* @param macro * @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_pproc_macro_t *scc_pproc_macro_table_set(scc_pproc_macro_table_t *pp,
scc_pp_macro_t *macro); scc_pproc_macro_t *macro);
/** /**
* @brief 查找宏定义 * @brief 查找宏定义
@@ -80,8 +79,8 @@ scc_pp_macro_t *scc_pp_macro_table_set(scc_pp_macro_table_t *pp,
* @param name 宏名称 * @param name 宏名称
* @return 找到的宏对象指针未找到返回NULL * @return 找到的宏对象指针未找到返回NULL
*/ */
scc_pp_macro_t *scc_pp_macro_table_get(scc_pp_macro_table_t *pp, scc_pproc_macro_t *scc_pproc_macro_table_get(scc_pproc_macro_table_t *pp,
const scc_cstring_t *name); const scc_cstring_t *name);
/** /**
* @brief 从预处理器中删除宏 * @brief 从预处理器中删除宏
@@ -89,9 +88,10 @@ scc_pp_macro_t *scc_pp_macro_table_get(scc_pp_macro_table_t *pp,
* @param name 宏名称 * @param name 宏名称
* @return 成功删除返回true未找到返回false * @return 成功删除返回true未找到返回false
*/ */
cbool scc_pp_macro_table_remove(scc_pp_macro_table_t *pp, cbool scc_pproc_macro_table_remove(scc_pproc_macro_table_t *pp,
const scc_cstring_t *name); 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__ */ #endif /* __SCC_PP_MACRO_H__ */

View File

@@ -28,18 +28,18 @@ typedef struct {
// 可能还需要保存当前位置等 // 可能还需要保存当前位置等
} scc_pproc_file_state_t; } scc_pproc_file_state_t;
typedef SCC_VEC(scc_pproc_file_state_t) scc_pproc_file_stack_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 SCC_VEC(scc_lexer_tok_ring_t *) scc_pproc_ring_vec_t;
typedef struct scc_pproc { typedef struct scc_pproc {
scc_lexer_tok_ring_t *cur_ring; scc_lexer_tok_ring_t *cur_ring;
scc_lexer_tok_ring_t expanded_ring;
scc_strpool_t strpool; 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_if_stack_t if_stack;
scc_pproc_file_stack_t file_stack; scc_pproc_file_stack_t file_stack;
scc_lexer_tok_vec_t cache;
int cache_pos;
scc_lexer_tok_ring_t ring; scc_lexer_tok_ring_t ring;
int ring_ref_count; int ring_ref_count;
} scc_pproc_t; } 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_drop(scc_pproc_t *pp);
void scc_pproc_handle_directive(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_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,
void scc_pproc_parse_macro_arguments(scc_pproc_t *pp, scc_lexer_tok_vec_t *args, int need_full);
scc_pproc_macro_list_t *args);
void scc_pproc_parse_function_macro(scc_pproc_t *pp, void scc_pproc_parse_function_macro(scc_pproc_t *pp,
const scc_lexer_tok_t *ident); const scc_lexer_tok_t *ident);
void scc_pproc_parse_object_macro(scc_pproc_t *pp, void scc_pproc_parse_object_macro(scc_pproc_t *pp,

View File

@@ -44,28 +44,34 @@ static inline int keyword_cmp(const char *name, int len) {
return -1; // Not a keyword. return -1; // Not a keyword.
} }
void scc_pproc_parse_macro_arguments(scc_pproc_t *pp, void scc_pproc_parse_macro_arguments(scc_lexer_tok_ring_t *ring,
scc_pproc_macro_list_t *args) { scc_lexer_tok_vec_t *args, int need_full) {
Assert(pp != null && args != null); Assert(ring != null && args != null);
scc_lexer_tok_t tok = {0}; scc_lexer_tok_t tok = {0};
scc_vec_init(*args);
int depth = 0; int depth = 0;
int ok;
do { do {
scc_lexer_next_non_blank(pp->cur_ring, &tok); scc_ring_next_consume(*ring, tok, ok);
if (tok.type == SCC_TOK_L_PAREN) { if (!ok) {
depth++; return;
} else if (tok.type == SCC_TOK_R_PAREN) { }
// scc_lexer_next_non_blank(ring, &tok);
if (tok.type == SCC_TOK_R_PAREN) {
depth--; depth--;
} }
if (depth > 1) { if (depth > 0 || need_full) {
scc_vec_push(*args, tok); scc_vec_push(*args, tok);
} else { } else {
scc_lexer_tok_drop(&tok); scc_lexer_tok_drop(&tok);
} }
if (tok.type == SCC_TOK_L_PAREN) {
depth++;
}
} while (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; int ok;
scc_lexer_tok_t tok; scc_lexer_tok_t tok;
ok = scc_lexer_next_non_blank(pp->cur_ring, &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, void scc_pproc_parse_function_macro(scc_pproc_t *pp,
const scc_lexer_tok_t *ident) { const scc_lexer_tok_t *ident) {
scc_pproc_macro_list_t args; scc_lexer_tok_vec_t args;
scc_pproc_parse_macro_arguments(pp, &args); scc_vec_init(args);
scc_pp_macro_t *macro = scc_pproc_parse_macro_arguments(pp->cur_ring, &args, false);
scc_pp_macro_new(&ident->lexeme, SCC_PP_MACRO_FUNCTION); scc_pproc_macro_t *macro =
scc_pproc_macro_new(&ident->lexeme, SCC_PP_MACRO_FUNCTION);
/* /*
check and set params check and set params
1. identifier-list(opt) 1. identifier-list(opt)
2. ... 2. ...
3. identifier-list , ... 3. identifier-list , ...
*/ */
int idx = 0;
scc_vec_foreach(args, i) { scc_vec_foreach(args, i) {
scc_lexer_tok_t *arg = &scc_vec_at(args, i); scc_lexer_tok_t *arg = &scc_vec_at(args, i);
if (arg->type == SCC_TOK_COMMA) { if (arg->type == SCC_TOK_COMMA) {
scc_lexer_tok_drop(arg); scc_lexer_tok_drop(arg);
if (i % 2 != 1) { if (idx++ % 2 != 1) {
LOG_FATAL("ERROR"); LOG_FATAL("ERROR");
} }
} else if (arg->type == SCC_TOK_IDENT) { } else if (arg->type == SCC_TOK_IDENT) {
if (i % 2 != 0) { if (idx++ % 2 != 0) {
LOG_FATAL("ERROR"); LOG_FATAL("ERROR");
} }
scc_vec_push(macro->params, *arg); scc_vec_push(macro->params, *arg);
} else if (arg->type == SCC_TOK_ELLIPSIS) { } else if (arg->type == SCC_TOK_ELLIPSIS) {
if (i % 2 != 0) { if (idx++ % 2 != 0) {
LOG_FATAL("ERROR"); LOG_FATAL("ERROR");
} }
scc_cstring_t va_args = scc_cstring_from_cstr("__VA_ARGS__"); scc_cstring_t va_args = scc_cstring_from_cstr("__VA_ARGS__");
scc_cstring_free(&arg->lexeme); scc_cstring_free(&arg->lexeme);
arg->lexeme = va_args; arg->lexeme = va_args;
scc_vec_push(macro->params, *arg); 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 { } else {
LOG_FATAL("ERROR"); LOG_FATAL("ERROR");
} }
} }
fill_replacements(pp, macro); 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, void scc_pproc_parse_object_macro(scc_pproc_t *pp,
const scc_lexer_tok_t *ident) { const scc_lexer_tok_t *ident) {
scc_pp_macro_t *macro = scc_pproc_macro_t *macro =
scc_pp_macro_new(&ident->lexeme, SCC_PP_MACRO_OBJECT); scc_pproc_macro_new(&ident->lexeme, SCC_PP_MACRO_OBJECT);
fill_replacements(pp, macro); 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"); LOG_ERROR("expected identifier");
goto ERROR; 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_tok_drop(&tok);
scc_lexer_next_non_blank(pp->cur_ring, &tok); scc_lexer_next_non_blank(pp->cur_ring, &tok);
if (tok.type != SCC_TOK_ENDLINE) { if (tok.type != SCC_TOK_ENDLINE) {
@@ -254,7 +266,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
goto ERROR; goto ERROR;
} }
scc_lexer_tok_drop(&tok); scc_lexer_tok_drop(&tok);
break; return;
} }
case SCC_PP_TOK_INCLUDE: case SCC_PP_TOK_INCLUDE:
case SCC_PP_TOK_IF: case SCC_PP_TOK_IF:

View File

@@ -1,32 +1,263 @@
#include <pproc_expand.h>
#include <scc_pproc.h> #include <scc_pproc.h>
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; int need_space = 0; // 是否需要插入空格
for (usize i = 0; i < arg_tokens->size; i++) {
void scc_pproc_expand_macro(scc_pproc_t *pp, const scc_pp_macro_t *macro) { scc_lexer_tok_t *tok = &scc_vec_at(*arg_tokens, i);
if (macro->type == SCC_PP_MACRO_NONE) { if (tok->type == SCC_TOK_BLANK) {
UNREACHABLE(); need_space = 1; // 标记遇到空白
} continue;
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);
} }
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 = &ring;
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, &macro->name)) {
scc_vec_push(expand_ctx->output, tok);
continue;
}
expand_ctx->need_rescan = true;
scc_pproc_macro_t *expanded_macro =
scc_pproc_macro_new(&macro->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, &macro->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) {}
} }

View File

@@ -1,9 +1,9 @@
#include <pproc_macro.h> #include <pproc_macro.h>
// 创建宏对象 // 创建宏对象
scc_pp_macro_t *scc_pp_macro_new(const scc_cstring_t *name, scc_pproc_macro_t *scc_pproc_macro_new(const scc_cstring_t *name,
scc_pp_macro_type_t type) { scc_pproc_macro_type_t type) {
scc_pp_macro_t *macro = scc_malloc(sizeof(scc_pp_macro_t)); scc_pproc_macro_t *macro = scc_malloc(sizeof(scc_pproc_macro_t));
if (!macro) { if (!macro) {
LOG_ERROR("Failed to allocate memory for macro"); LOG_ERROR("Failed to allocate memory for macro");
return null; 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) if (!macro)
return; 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, cbool scc_pproc_add_object_macro(scc_pproc_macro_table_t *macros,
const scc_cstring_t *name, const scc_cstring_t *name,
const scc_pproc_macro_list_t *replacement) { const scc_lexer_tok_vec_t *replacement) {
if (!macros || !name || !replacement) if (!macros || !name || !replacement)
return false; 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) if (!macro)
return false; return false;
macro->replaces = *replacement; macro->replaces = *replacement;
// 检查是否已存在同名宏 // 检查是否已存在同名宏
scc_pp_macro_t *existing = scc_hashtable_get(&macros->table, &macro->name); scc_pproc_macro_t *existing =
scc_hashtable_get(&macros->table, &macro->name);
if (existing) { if (existing) {
LOG_WARN("Redefining macro: %s", scc_cstring_as_cstr(&macro->name)); LOG_WARN("Redefining macro: %s", scc_cstring_as_cstr(&macro->name));
scc_pp_macro_drop(existing); scc_pproc_macro_drop(existing);
} }
scc_hashtable_set(&macros->table, &macro->name, macro); scc_hashtable_set(&macros->table, &macro->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, cbool scc_pproc_add_function_macro(scc_pproc_macro_table_t *macros,
const scc_cstring_t *name, const scc_cstring_t *name,
const scc_pproc_macro_list_t *params, const scc_lexer_tok_vec_t *params,
const scc_pproc_macro_list_t *replacement) { const scc_lexer_tok_vec_t *replacement) {
if (!macros || !name || !params || !replacement) if (!macros || !name || !params || !replacement)
return false; 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) if (!macro)
return false; return false;
@@ -80,10 +81,11 @@ cbool scc_pp_add_function_macro(scc_pp_macro_table_t *macros,
macro->replaces = *replacement; macro->replaces = *replacement;
// 检查是否已存在同名宏 // 检查是否已存在同名宏
scc_pp_macro_t *existing = scc_hashtable_get(&macros->table, &macro->name); scc_pproc_macro_t *existing =
scc_hashtable_get(&macros->table, &macro->name);
if (existing) { if (existing) {
LOG_WARN("Redefining macro: %s", scc_cstring_as_cstr(&macro->name)); LOG_WARN("Redefining macro: %s", scc_cstring_as_cstr(&macro->name));
scc_pp_macro_drop(existing); scc_pproc_macro_drop(existing);
} }
scc_hashtable_set(&macros->table, &macro->name, macro); scc_hashtable_set(&macros->table, &macro->name, macro);
@@ -92,30 +94,30 @@ cbool scc_pp_add_function_macro(scc_pp_macro_table_t *macros,
/// marco_table /// marco_table
scc_pp_macro_t *scc_pp_macro_table_set(scc_pp_macro_table_t *pp, scc_pproc_macro_t *scc_pproc_macro_table_set(scc_pproc_macro_table_t *pp,
scc_pp_macro_t *macro) { scc_pproc_macro_t *macro) {
Assert(pp != null && macro != null); Assert(pp != null && macro != null);
return scc_hashtable_set(&pp->table, &macro->name, macro); return scc_hashtable_set(&pp->table, &macro->name, macro);
} }
// 查找宏定义 // 查找宏定义
scc_pp_macro_t *scc_pp_macro_table_get(scc_pp_macro_table_t *pp, scc_pproc_macro_t *scc_pproc_macro_table_get(scc_pproc_macro_table_t *pp,
const scc_cstring_t *name) { const scc_cstring_t *name) {
return scc_hashtable_get(&pp->table, name); return scc_hashtable_get(&pp->table, name);
} }
// 从预处理器中删除宏 // 从预处理器中删除宏
cbool scc_pp_macro_table_remove(scc_pp_macro_table_t *pp, cbool scc_pproc_macro_table_remove(scc_pproc_macro_table_t *pp,
const scc_cstring_t *name) { const scc_cstring_t *name) {
if (!pp || !name) if (!pp || !name)
return false; 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) if (!macro)
return false; return false;
scc_hashtable_del(&pp->table, name); scc_hashtable_del(&pp->table, name);
scc_pp_macro_drop(macro); scc_pproc_macro_drop(macro);
return true; 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)); 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); Assert(macros != null);
macros->table.hash_func = hash_func; macros->table.hash_func = hash_func;
macros->table.key_cmp = hash_cmp; 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) { static int macro_free(const void *key, void *value, void *context) {
(void)key; (void)key;
(void)context; (void)context;
scc_pp_macro_drop(value); scc_pproc_macro_drop(value);
return 0; 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); Assert(macros != null);
scc_hashtable_foreach(&macros->table, macro_free, null); scc_hashtable_foreach(&macros->table, macro_free, null);
scc_hashtable_drop(&macros->table); scc_hashtable_drop(&macros->table);

View File

@@ -5,30 +5,44 @@ static int pproc_next(scc_pproc_t *pp, scc_lexer_tok_t *out) {
scc_lexer_tok_t tok = {0}; scc_lexer_tok_t tok = {0};
int ok = 0; int ok = 0;
CONTINUE: CONTINUE:
if (scc_vec_size(pp->cache)) { if (pp->expanded_ring.cap) {
// use cache? scc_ring_next_consume(pp->expanded_ring, *out, ok);
*out = scc_vec_at(pp->cache, pp->cache_pos); if (ok == false) {
pp->cache_pos++; scc_ring_free(pp->expanded_ring);
if (pp->cache_pos == scc_vec_size(pp->cache)) { goto CONTINUE;
pp->cache_pos = 0; } else {
scc_vec_free(pp->cache); return true;
} }
return true;
} }
scc_ring_peek(*stream, tok, ok); 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 # // parse to #
scc_pproc_handle_directive(pp); scc_pproc_handle_directive(pp);
goto CONTINUE; goto CONTINUE;
} else if (tok.type == SCC_TOK_IDENT) { }
pp->at_line_start = false;
if (tok.type == SCC_TOK_IDENT) {
// maybe expanded // maybe expanded
scc_pp_macro_t *macro = scc_pproc_macro_t *macro =
scc_pp_macro_table_get(&pp->macro_table, &tok.lexeme); scc_pproc_macro_table_get(&pp->macro_table, &tok.lexeme);
scc_ring_next_consume(*stream, *out, ok);
if (macro == null) { if (macro == null) {
scc_ring_next_consume(*stream, *out, ok);
return ok; return ok;
} }
scc_pproc_expand_macro(pp, macro); scc_pproc_expand_by_src(pp, macro);
goto CONTINUE; goto CONTINUE;
} else { } else {
// continue // continue
@@ -41,11 +55,11 @@ CONTINUE:
void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input) { void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input) {
Assert(pp != null && input != null); Assert(pp != null && input != null);
pp->cur_ring = input; 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->if_stack);
scc_vec_init(pp->file_stack); scc_vec_init(pp->file_stack);
scc_vec_init(pp->cache); pp->at_line_start = true;
pp->cache_pos = 0;
} }
static cbool fill_token(scc_lexer_tok_t *tok, void *userdata) { 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) if (pp == null)
return; return;
scc_lexer_drop_ring(pp->cur_ring); scc_lexer_drop_ring(pp->cur_ring);
scc_pp_macro_table_drop(&pp->macro_table); scc_pproc_macro_table_drop(&pp->macro_table);
} }

View File

@@ -88,6 +88,7 @@ static void test_define_object_macro_backspace(void) {
static void test_define_function_macro(void) { static void test_define_function_macro(void) {
TEST_CASE("function-like macro"); 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 ADD( a , b ) a + b\nADD(1, 2)\n", "1 + 2\n");
CHECK_PP_OUTPUT_EXACT( CHECK_PP_OUTPUT_EXACT(
"#define MAX(a,b) ((a) > (b) ? (a) : (b))\nMAX(10, 20)\n", "#define MAX(a,b) ((a) > (b) ? (a) : (b))\nMAX(10, 20)\n",
"((10) > (20) ? (10) : (20))\n"); "((10) > (20) ? (10) : (20))\n");
@@ -99,6 +100,8 @@ static void test_define_stringify_operator(void) {
"\"hello\"\n"); "\"hello\"\n");
CHECK_PP_OUTPUT_EXACT("#define STR(x) #x\nSTR(test value)\n", CHECK_PP_OUTPUT_EXACT("#define STR(x) #x\nSTR(test value)\n",
"\"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) { static void test_define_concat_operator(void) {
@@ -206,7 +209,7 @@ static void test_error_cases(void) {
static void test_edge_cases(void) { static void test_edge_cases(void) {
TEST_CASE("empty macro"); 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"); TEST_CASE("macro with only spaces");
CHECK_PP_OUTPUT_EXACT("#define SPACE \nSPACE\n", "\n"); CHECK_PP_OUTPUT_EXACT("#define SPACE \nSPACE\n", "\n");

View File

@@ -51,7 +51,8 @@
break; \ break; \
} \ } \
usize phys_tail = scc_ring_phys(ring, (ring).tail); \ usize phys_tail = scc_ring_phys(ring, (ring).tail); \
if (!(ring).fill(&(ring).data[phys_tail], (ring).userdata)) { \ if ((ring).fill == null || \
!(ring).fill(&(ring).data[phys_tail], (ring).userdata)) { \
ok = 0; \ ok = 0; \
break; \ break; \
} \ } \
@@ -79,6 +80,17 @@
(ring).userdata = (_userdata); \ (ring).userdata = (_userdata); \
} while (0) } while (0)
#define scc_ring_by_buffer(ring, buffer, size) \
do { \
(ring).data = (buffer); \
(ring).cap = (size); \
(ring).head = 0; \
(ring).probe = 0; \
(ring).tail = (size); \
(ring).fill = null; \
(ring).userdata = null; \
} while (0)
/** /**
* @brief 释放环形缓冲区内存 * @brief 释放环形缓冲区内存
* @param ring 环形缓冲区变量 * @param ring 环形缓冲区变量

View File

@@ -194,6 +194,11 @@ static inline char *scc_cstring_as_cstr(const scc_cstring_t *str) {
return str->data; return str->data;
} }
static inline int scc_cstring_cmp(const scc_cstring_t *str1,
const scc_cstring_t *str2) {
return scc_strcmp(scc_cstring_as_cstr(str1), scc_cstring_as_cstr(str2));
}
static inline char *scc_cstring_move_cstr(scc_cstring_t *str) { static inline char *scc_cstring_move_cstr(scc_cstring_t *str) {
if (str == null || str->data == null) { if (str == null || str->data == null) {
return null; return null;