feat(argparse): 添加列表类型参数支持
新增 scc_argparse_list_t 类型用于处理多个字符串值的参数, 并添加相应的配置函数 scc_argparse_spec_setup_list。 fix(lexer): 调整关键字标记处理逻辑 将关键字子类型从 SCC_TOK_SUBTYPE_KEYWORD 改为 SCC_TOK_SUBTYPE_IDENTIFIER,并移除相关的枚举定义。 refactor(lexer): 优化跳过换行功能实现 修改 scc_lexer_skip_until_newline 函数实现,改进循环逻辑和错误处理。 feat(pproc): 完善预处理器条件编译支持 重构条件编译状态管理,添加对 #ifdef、#ifndef、#elifdef、 #else、#endif 等指令的支持,并实现嵌套条件处理。 refactor(pproc): 优化文件包含路径处理 添加最大包含深度限制,改进包含路径添加功能, 修复文件状态结构命名。 docs(log): 更新日志模块标准库依赖 调整 stdarg.h 包含逻辑,更新编译器内置宏定义名称。 feat(core): 扩展核心类型定义 添加基础数据类型别名定义,完善类型系统支持。 feat(main): 实现命令行包含路径参数 添加 -I/--include 参数支持,允许用户指定额外的头文件搜索路径。
This commit is contained in:
@@ -43,6 +43,7 @@ typedef enum scc_argparse_err {
|
|||||||
SCC_ARGPARSE_ERR_UNKNOWN_VALUE,
|
SCC_ARGPARSE_ERR_UNKNOWN_VALUE,
|
||||||
} scc_argparse_err_t;
|
} scc_argparse_err_t;
|
||||||
|
|
||||||
|
typedef SCC_VEC(const char *) scc_argparse_list_t;
|
||||||
// 约束规范结构体
|
// 约束规范结构体
|
||||||
struct scc_argparse_spec {
|
struct scc_argparse_spec {
|
||||||
scc_argparse_val_type_t value_type; // 值类型
|
scc_argparse_val_type_t value_type; // 值类型
|
||||||
@@ -50,12 +51,13 @@ struct scc_argparse_spec {
|
|||||||
|
|
||||||
// 存储位置
|
// 存储位置
|
||||||
union {
|
union {
|
||||||
cbool *bool_store; // 布尔值存储
|
cbool *bool_store; // 布尔值存储
|
||||||
int *int_store; // 整数存储
|
int *int_store; // 整数存储
|
||||||
float *float_store; // 浮点数存储
|
float *float_store; // 浮点数存储
|
||||||
const char **str_store; // 字符串存储
|
const char **str_store; // 字符串存储
|
||||||
char **str_alloc_store; // 字符串存储(使用alloc,需要free)
|
char **str_alloc_store; // 字符串存储(使用alloc,需要free)
|
||||||
void **ptr_store; // 通用指针存储
|
void **ptr_store; // 通用指针存储
|
||||||
|
scc_argparse_list_t *vec_store; // 新增:指向字符串向量的指针
|
||||||
} store;
|
} store;
|
||||||
|
|
||||||
// 枚举值约束
|
// 枚举值约束
|
||||||
@@ -215,6 +217,15 @@ static inline void scc_argparse_spec_setup_choices(scc_argparse_spec_t *spec,
|
|||||||
spec->choices.count = count;
|
spec->choices.count = count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 添加设置列表的辅助函数(内联)
|
||||||
|
static inline void scc_argparse_spec_setup_list(scc_argparse_spec_t *spec,
|
||||||
|
scc_argparse_list_t *vec) {
|
||||||
|
spec->value_type = SCC_ARGPARSE_VAL_TYPE_LIST;
|
||||||
|
spec->store.vec_store = vec;
|
||||||
|
// 自动设置 flag_takes_multiple 为 true,因为列表需要多个值
|
||||||
|
spec->flag_takes_multiple = true;
|
||||||
|
}
|
||||||
|
|
||||||
#define SCC_ARGPARSE_MACRO_SETTER(attr) \
|
#define SCC_ARGPARSE_MACRO_SETTER(attr) \
|
||||||
static inline void scc_argparse_spec_set_##attr(scc_argparse_spec_t *spec, \
|
static inline void scc_argparse_spec_set_##attr(scc_argparse_spec_t *spec, \
|
||||||
cbool flag) { \
|
cbool flag) { \
|
||||||
|
|||||||
@@ -63,6 +63,10 @@ static inline void parse_cmd(scc_optparse_t *optparse,
|
|||||||
min_args = 0;
|
min_args = 0;
|
||||||
max_args = 0;
|
max_args = 0;
|
||||||
break;
|
break;
|
||||||
|
case SCC_ARGPARSE_VAL_TYPE_LIST: // 列表类型
|
||||||
|
min_args = 1;
|
||||||
|
max_args = 65535; // FIXME maybe INT_MAX ?
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
min_args = 0;
|
min_args = 0;
|
||||||
max_args = 0;
|
max_args = 0;
|
||||||
@@ -161,6 +165,10 @@ static int handle_option(scc_argparse_context_t *ctx, scc_argparse_t *parser) {
|
|||||||
return SCC_ARGPARSE_ERR_PNT_DEFAULT;
|
return SCC_ARGPARSE_ERR_PNT_DEFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (parser->need_version && scc_strcmp(opt->long_name, "version") == 0) {
|
||||||
|
// TODO default version print
|
||||||
|
}
|
||||||
|
|
||||||
if (opt->spec.flag_store_as_count) {
|
if (opt->spec.flag_store_as_count) {
|
||||||
(*opt->spec.store.int_store)++;
|
(*opt->spec.store.int_store)++;
|
||||||
}
|
}
|
||||||
@@ -169,8 +177,14 @@ static int handle_option(scc_argparse_context_t *ctx, scc_argparse_t *parser) {
|
|||||||
*opt->spec.store.bool_store = true;
|
*opt->spec.store.bool_store = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->result.value) {
|
if (!ctx->result.value) {
|
||||||
opt->spec.raw_value = ctx->result.value;
|
return SCC_ARGPARSE_ERR_NONE;
|
||||||
|
}
|
||||||
|
opt->spec.raw_value = ctx->result.value;
|
||||||
|
|
||||||
|
if (opt->spec.value_type == SCC_ARGPARSE_VAL_TYPE_LIST) {
|
||||||
|
scc_vec_push(*opt->spec.store.vec_store, ctx->result.value);
|
||||||
|
} else {
|
||||||
*opt->spec.store.str_store = ctx->result.value;
|
*opt->spec.store.str_store = ctx->result.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,44 +42,44 @@ typedef enum scc_cstd {
|
|||||||
// WARNING: Using Binary Search To Fast Find Keyword
|
// WARNING: Using Binary Search To Fast Find Keyword
|
||||||
// 你必须确保其中是按照字典序排列
|
// 你必须确保其中是按照字典序排列
|
||||||
#define SCC_CKEYWORD_TABLE \
|
#define SCC_CKEYWORD_TABLE \
|
||||||
X(asm , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_ASM , SCC_CEXT_SCC) \
|
X(asm , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_ASM , SCC_CEXT_SCC) \
|
||||||
X(atomic , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_ATOMIC , SCC_CEXT_SCC) \
|
X(atomic , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_ATOMIC , SCC_CEXT_SCC) \
|
||||||
X(auto , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_AUTO , SCC_CEXT_SCC) \
|
X(auto , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_AUTO , SCC_CEXT_SCC) \
|
||||||
X(bool , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_BOOL , SCC_CEXT_SCC) \
|
X(bool , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_BOOL , SCC_CEXT_SCC) \
|
||||||
X(break , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_BREAK , SCC_CSTD_C89) \
|
X(break , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_BREAK , SCC_CSTD_C89) \
|
||||||
X(case , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_CASE , SCC_CSTD_C89) \
|
X(case , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_CASE , SCC_CSTD_C89) \
|
||||||
X(char , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_CHAR , SCC_CSTD_C89) \
|
X(char , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_CHAR , SCC_CSTD_C89) \
|
||||||
X(complex , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_COMPLEX , SCC_CEXT_SCC) \
|
X(complex , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_COMPLEX , SCC_CEXT_SCC) \
|
||||||
X(const , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_CONST , SCC_CSTD_C89) \
|
X(const , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_CONST , SCC_CSTD_C89) \
|
||||||
X(continue , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_CONTINUE , SCC_CSTD_C89) \
|
X(continue , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_CONTINUE , SCC_CSTD_C89) \
|
||||||
X(default , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_DEFAULT , SCC_CSTD_C89) \
|
X(default , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_DEFAULT , SCC_CSTD_C89) \
|
||||||
X(do , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_DO , SCC_CSTD_C89) \
|
X(do , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_DO , SCC_CSTD_C89) \
|
||||||
X(double , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_DOUBLE , SCC_CSTD_C89) \
|
X(double , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_DOUBLE , SCC_CSTD_C89) \
|
||||||
X(else , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_ELSE , SCC_CSTD_C89) \
|
X(else , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_ELSE , SCC_CSTD_C89) \
|
||||||
X(enum , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_ENUM , SCC_CSTD_C89) \
|
X(enum , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_ENUM , SCC_CSTD_C89) \
|
||||||
X(extern , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_EXTERN , SCC_CSTD_C89) \
|
X(extern , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_EXTERN , SCC_CSTD_C89) \
|
||||||
X(float , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_FLOAT , SCC_CSTD_C89) \
|
X(float , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_FLOAT , SCC_CSTD_C89) \
|
||||||
X(for , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_FOR , SCC_CSTD_C89) \
|
X(for , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_FOR , SCC_CSTD_C89) \
|
||||||
X(goto , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_GOTO , SCC_CSTD_C89) \
|
X(goto , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_GOTO , SCC_CSTD_C89) \
|
||||||
X(if , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_IF , SCC_CSTD_C89) \
|
X(if , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_IF , SCC_CSTD_C89) \
|
||||||
X(inline , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_INLINE , SCC_CSTD_C99) \
|
X(inline , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_INLINE , SCC_CSTD_C99) \
|
||||||
X(int , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_INT , SCC_CSTD_C89) \
|
X(int , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_INT , SCC_CSTD_C89) \
|
||||||
X(long , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_LONG , SCC_CSTD_C89) \
|
X(long , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_LONG , SCC_CSTD_C89) \
|
||||||
X(register , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_REGISTER , SCC_CSTD_C89) \
|
X(register , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_REGISTER , SCC_CSTD_C89) \
|
||||||
X(restrict , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_RESTRICT , SCC_CSTD_C99) \
|
X(restrict , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_RESTRICT , SCC_CSTD_C99) \
|
||||||
X(return , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_RETURN , SCC_CSTD_C89) \
|
X(return , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_RETURN , SCC_CSTD_C89) \
|
||||||
X(short , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_SHORT , SCC_CSTD_C89) \
|
X(short , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_SHORT , SCC_CSTD_C89) \
|
||||||
X(signed , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_SIGNED , SCC_CSTD_C89) \
|
X(signed , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_SIGNED , SCC_CSTD_C89) \
|
||||||
X(sizeof , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_SIZEOF , SCC_CSTD_C89) \
|
X(sizeof , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_SIZEOF , SCC_CSTD_C89) \
|
||||||
X(static , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_STATIC , SCC_CSTD_C89) \
|
X(static , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_STATIC , SCC_CSTD_C89) \
|
||||||
X(struct , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_STRUCT , SCC_CSTD_C89) \
|
X(struct , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_STRUCT , SCC_CSTD_C89) \
|
||||||
X(switch , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_SWITCH , SCC_CSTD_C89) \
|
X(switch , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_SWITCH , SCC_CSTD_C89) \
|
||||||
X(typedef , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_TYPEDEF , SCC_CSTD_C89) \
|
X(typedef , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_TYPEDEF , SCC_CSTD_C89) \
|
||||||
X(union , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_UNION , SCC_CSTD_C89) \
|
X(union , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_UNION , SCC_CSTD_C89) \
|
||||||
X(unsigned , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_UNSIGNED , SCC_CSTD_C89) \
|
X(unsigned , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_UNSIGNED , SCC_CSTD_C89) \
|
||||||
X(void , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_VOID , SCC_CSTD_C89) \
|
X(void , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_VOID , SCC_CSTD_C89) \
|
||||||
X(volatile , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_VOLATILE , SCC_CSTD_C89) \
|
X(volatile , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_VOLATILE , SCC_CSTD_C89) \
|
||||||
X(while , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_WHILE , SCC_CSTD_C89) \
|
X(while , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_WHILE , SCC_CSTD_C89) \
|
||||||
// KEYWORD_TABLE
|
// KEYWORD_TABLE
|
||||||
|
|
||||||
#define SCC_CTOK_TABLE \
|
#define SCC_CTOK_TABLE \
|
||||||
@@ -163,7 +163,6 @@ typedef enum scc_tok_type {
|
|||||||
|
|
||||||
typedef enum scc_tok_subtype {
|
typedef enum scc_tok_subtype {
|
||||||
SCC_TOK_SUBTYPE_INVALID, // 错误占位
|
SCC_TOK_SUBTYPE_INVALID, // 错误占位
|
||||||
SCC_TOK_SUBTYPE_KEYWORD, // 关键字
|
|
||||||
SCC_TOK_SUBTYPE_OPERATOR, // 操作符
|
SCC_TOK_SUBTYPE_OPERATOR, // 操作符
|
||||||
SCC_TOK_SUBTYPE_IDENTIFIER, // 标识符
|
SCC_TOK_SUBTYPE_IDENTIFIER, // 标识符
|
||||||
SCC_TOK_SUBTYPE_LITERAL, // 字面量
|
SCC_TOK_SUBTYPE_LITERAL, // 字面量
|
||||||
|
|||||||
@@ -28,11 +28,14 @@ static inline cbool scc_lexer_next_non_blank(scc_lexer_tok_ring_t *stream,
|
|||||||
static inline void scc_lexer_skip_until_newline(scc_lexer_tok_ring_t *stream) {
|
static inline void scc_lexer_skip_until_newline(scc_lexer_tok_ring_t *stream) {
|
||||||
scc_lexer_tok_t tok;
|
scc_lexer_tok_t tok;
|
||||||
cbool ok;
|
cbool ok;
|
||||||
while (scc_lexer_peek_non_blank(stream, &tok)) {
|
while (1) {
|
||||||
if (tok.type == SCC_TOK_ENDLINE)
|
|
||||||
break;
|
|
||||||
scc_ring_next_consume(*stream, tok, ok);
|
scc_ring_next_consume(*stream, tok, ok);
|
||||||
|
if (!ok)
|
||||||
|
break;
|
||||||
|
scc_tok_type_t type = tok.type;
|
||||||
scc_lexer_tok_drop(&tok);
|
scc_lexer_tok_drop(&tok);
|
||||||
|
if (type == SCC_TOK_ENDLINE)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,18 +15,19 @@
|
|||||||
|
|
||||||
// 条件编译状态栈
|
// 条件编译状态栈
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int active; // 当前层级是否有效(即应该输出 token)
|
cbool found_true;
|
||||||
int skip; // 当前层级是否跳过(即不输出 token)
|
cbool seen_else;
|
||||||
// 可根据需要增加状态,如 #if 的结果、#elif 已执行等
|
cbool active;
|
||||||
} scc_pproc_if_state_t;
|
} scc_pproc_if_t;
|
||||||
typedef SCC_VEC(scc_pproc_if_state_t) scc_pproc_if_stack_t;
|
typedef SCC_VEC(scc_pproc_if_t) scc_pproc_if_stack_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
scc_sstream_t sstream;
|
scc_sstream_t sstream;
|
||||||
scc_lexer_t lexer;
|
scc_lexer_t lexer;
|
||||||
scc_lexer_tok_ring_t *ring;
|
scc_lexer_tok_ring_t *ring;
|
||||||
} scc_pproc_file_state_t;
|
} scc_pproc_file_t;
|
||||||
typedef SCC_VEC(scc_pproc_file_state_t *) scc_pproc_file_stack_t;
|
typedef SCC_VEC(scc_pproc_file_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 SCC_VEC(scc_cstring_t) scc_pproc_cstr_vec_t;
|
typedef SCC_VEC(scc_cstring_t) scc_pproc_cstr_vec_t;
|
||||||
|
|
||||||
@@ -36,6 +37,7 @@ typedef struct scc_pproc {
|
|||||||
scc_lexer_tok_ring_t expanded_ring;
|
scc_lexer_tok_ring_t expanded_ring;
|
||||||
scc_strpool_t strpool;
|
scc_strpool_t strpool;
|
||||||
int at_line_start;
|
int at_line_start;
|
||||||
|
int enable;
|
||||||
|
|
||||||
scc_pproc_cstr_vec_t include_paths;
|
scc_pproc_cstr_vec_t include_paths;
|
||||||
scc_pproc_macro_table_t macro_table;
|
scc_pproc_macro_table_t macro_table;
|
||||||
@@ -44,12 +46,26 @@ typedef struct scc_pproc {
|
|||||||
|
|
||||||
scc_lexer_tok_ring_t ring;
|
scc_lexer_tok_ring_t ring;
|
||||||
int ring_ref_count;
|
int ring_ref_count;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
int max_include_depth;
|
||||||
|
} config;
|
||||||
} scc_pproc_t;
|
} scc_pproc_t;
|
||||||
|
|
||||||
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);
|
||||||
scc_lexer_tok_ring_t *scc_pproc_to_ring(scc_pproc_t *pp, int ring_size);
|
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);
|
||||||
|
|
||||||
|
static inline void scc_pproc_add_include_path(scc_pproc_t *pp,
|
||||||
|
const scc_cstring_t *path) {
|
||||||
|
scc_vec_push(pp->include_paths, scc_cstring_copy(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void scc_pproc_add_include_path_cstr(scc_pproc_t *pp,
|
||||||
|
const char *path) {
|
||||||
|
scc_vec_push(pp->include_paths, scc_cstring_from_cstr(path));
|
||||||
|
}
|
||||||
|
|
||||||
void scc_pproc_handle_directive(scc_pproc_t *pp);
|
void scc_pproc_handle_directive(scc_pproc_t *pp);
|
||||||
void scc_pproc_parse_include(scc_pproc_t *pp, scc_lexer_tok_t *include_tok);
|
void scc_pproc_parse_include(scc_pproc_t *pp, scc_lexer_tok_t *include_tok);
|
||||||
void scc_pproc_parse_macro_arguments(scc_lexer_tok_ring_t *ring,
|
void scc_pproc_parse_macro_arguments(scc_lexer_tok_ring_t *ring,
|
||||||
|
|||||||
@@ -208,10 +208,11 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
|
|||||||
scc_lexer_tok_t tok = {0};
|
scc_lexer_tok_t tok = {0};
|
||||||
int ok = 0;
|
int ok = 0;
|
||||||
scc_ring_next(*pp->cur_ring, tok, ok);
|
scc_ring_next(*pp->cur_ring, tok, ok);
|
||||||
|
Assert(ok == true && tok.type == SCC_TOK_SHARP);
|
||||||
scc_lexer_tok_drop(&tok);
|
scc_lexer_tok_drop(&tok);
|
||||||
|
|
||||||
if (!scc_lexer_next_non_blank(pp->cur_ring, &tok) ||
|
if (!scc_lexer_next_non_blank(pp->cur_ring, &tok) ||
|
||||||
tok.type != SCC_TOK_IDENT) {
|
scc_get_tok_subtype(tok.type) != SCC_TOK_SUBTYPE_IDENTIFIER) {
|
||||||
scc_lexer_tok_drop(&tok);
|
scc_lexer_tok_drop(&tok);
|
||||||
LOG_ERROR("Invalid preprocessor directive");
|
LOG_ERROR("Invalid preprocessor directive");
|
||||||
goto ERROR;
|
goto ERROR;
|
||||||
@@ -223,8 +224,34 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
|
|||||||
LOG_ERROR("Expected preprocessor keyword, got %s", tok.lexeme);
|
LOG_ERROR("Expected preprocessor keyword, got %s", tok.lexeme);
|
||||||
goto ERROR;
|
goto ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
scc_tok_type_t type = keywords[ret].tok_type;
|
scc_tok_type_t type = keywords[ret].tok_type;
|
||||||
|
|
||||||
|
if (scc_vec_size(pp->if_stack) != 0) {
|
||||||
|
scc_pproc_if_t *top =
|
||||||
|
&scc_vec_at(pp->if_stack, scc_vec_size(pp->if_stack) - 1);
|
||||||
|
pp->enable = top->active;
|
||||||
|
}
|
||||||
|
// 非条件指令,且当前不活动时直接跳过整行
|
||||||
|
int is_conditional = 0;
|
||||||
|
switch (type) {
|
||||||
|
case SCC_PP_TOK_IFDEF:
|
||||||
|
case SCC_PP_TOK_IFNDEF:
|
||||||
|
case SCC_PP_TOK_ELIFDEF:
|
||||||
|
case SCC_PP_TOK_ELIFNDEF:
|
||||||
|
case SCC_PP_TOK_ELSE:
|
||||||
|
case SCC_PP_TOK_ENDIF:
|
||||||
|
case SCC_PP_TOK_IF:
|
||||||
|
case SCC_PP_TOK_ELIF:
|
||||||
|
is_conditional = 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!is_conditional && !pp->enable) {
|
||||||
|
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case SCC_PP_TOK_DEFINE: {
|
case SCC_PP_TOK_DEFINE: {
|
||||||
scc_lexer_tok_drop(&tok);
|
scc_lexer_tok_drop(&tok);
|
||||||
@@ -273,15 +300,118 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
|
|||||||
case SCC_PP_TOK_INCLUDE:
|
case SCC_PP_TOK_INCLUDE:
|
||||||
scc_pproc_parse_include(pp, &tok);
|
scc_pproc_parse_include(pp, &tok);
|
||||||
return;
|
return;
|
||||||
case SCC_PP_TOK_IF:
|
|
||||||
case SCC_PP_TOK_IFDEF:
|
case SCC_PP_TOK_IFDEF:
|
||||||
case SCC_PP_TOK_IFNDEF:
|
case SCC_PP_TOK_IFNDEF: {
|
||||||
case SCC_PP_TOK_ELSE:
|
// 解析标识符
|
||||||
case SCC_PP_TOK_ELIF:
|
if (!scc_lexer_next_non_blank(pp->cur_ring, &tok) ||
|
||||||
|
tok.type != SCC_TOK_IDENT) {
|
||||||
|
scc_lexer_tok_drop(&tok);
|
||||||
|
LOG_ERROR("expected identifier");
|
||||||
|
goto ERROR;
|
||||||
|
}
|
||||||
|
cbool defined =
|
||||||
|
(scc_pproc_macro_table_get(&pp->macro_table, &tok.lexeme) != null);
|
||||||
|
cbool condition = (type == SCC_PP_TOK_IFDEF) ? defined : !defined;
|
||||||
|
scc_lexer_tok_drop(&tok);
|
||||||
|
|
||||||
|
scc_pproc_if_t new_if;
|
||||||
|
new_if.found_true = condition;
|
||||||
|
new_if.seen_else = 0;
|
||||||
|
new_if.active = pp->enable ? condition : 0;
|
||||||
|
scc_vec_push(pp->if_stack, new_if);
|
||||||
|
|
||||||
|
pp->enable = new_if.active;
|
||||||
|
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||||
|
return;
|
||||||
|
}
|
||||||
case SCC_PP_TOK_ELIFDEF:
|
case SCC_PP_TOK_ELIFDEF:
|
||||||
case SCC_PP_TOK_ELIFNDEF:
|
case SCC_PP_TOK_ELIFNDEF: {
|
||||||
case SCC_PP_TOK_ENDIF:
|
if (scc_vec_size(pp->if_stack) == 0) {
|
||||||
goto ERROR;
|
LOG_ERROR("#elif without #if");
|
||||||
|
goto ERROR;
|
||||||
|
}
|
||||||
|
scc_pproc_if_t *top =
|
||||||
|
&scc_vec_at(pp->if_stack, scc_vec_size(pp->if_stack) - 1);
|
||||||
|
if (top->seen_else) {
|
||||||
|
LOG_ERROR("#elif after #else");
|
||||||
|
goto ERROR;
|
||||||
|
}
|
||||||
|
// 解析标识符
|
||||||
|
if (!scc_lexer_next_non_blank(pp->cur_ring, &tok) ||
|
||||||
|
tok.type != SCC_TOK_IDENT) {
|
||||||
|
scc_lexer_tok_drop(&tok);
|
||||||
|
LOG_ERROR("expected identifier");
|
||||||
|
goto ERROR;
|
||||||
|
}
|
||||||
|
int defined =
|
||||||
|
(scc_pproc_macro_table_get(&pp->macro_table, &tok.lexeme) != NULL);
|
||||||
|
int condition = (type == SCC_PP_TOK_ELIFDEF) ? defined : !defined;
|
||||||
|
scc_lexer_tok_drop(&tok);
|
||||||
|
|
||||||
|
if (top->found_true) {
|
||||||
|
// 前面已有真分支,本 elif 不激活
|
||||||
|
top->active = 0;
|
||||||
|
} else {
|
||||||
|
if (condition) {
|
||||||
|
top->active = 1;
|
||||||
|
top->found_true = 1;
|
||||||
|
} else {
|
||||||
|
top->active = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// seen_else 仍为 0
|
||||||
|
pp->enable = top->active;
|
||||||
|
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case SCC_PP_TOK_ELSE: {
|
||||||
|
if (scc_vec_size(pp->if_stack) == 0) {
|
||||||
|
LOG_ERROR("#else without #if");
|
||||||
|
goto ERROR;
|
||||||
|
}
|
||||||
|
scc_pproc_if_t *top =
|
||||||
|
&scc_vec_at(pp->if_stack, scc_vec_size(pp->if_stack) - 1);
|
||||||
|
if (top->seen_else) {
|
||||||
|
LOG_ERROR("multiple #else");
|
||||||
|
goto ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (top->found_true) {
|
||||||
|
top->active = 0;
|
||||||
|
} else {
|
||||||
|
top->active = 1;
|
||||||
|
top->found_true = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
top->seen_else = 1;
|
||||||
|
|
||||||
|
pp->enable = top->active;
|
||||||
|
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case SCC_PP_TOK_ENDIF: {
|
||||||
|
if (scc_vec_size(pp->if_stack) == 0) {
|
||||||
|
LOG_ERROR("#endif without #if");
|
||||||
|
} else {
|
||||||
|
scc_vec_pop(pp->if_stack);
|
||||||
|
}
|
||||||
|
if (scc_vec_size(pp->if_stack) == 0) {
|
||||||
|
pp->enable = 1;
|
||||||
|
} else {
|
||||||
|
pp->enable =
|
||||||
|
scc_vec_at(pp->if_stack, scc_vec_size(pp->if_stack) - 1).active;
|
||||||
|
}
|
||||||
|
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case SCC_PP_TOK_IF:
|
||||||
|
case SCC_PP_TOK_ELIF:
|
||||||
|
// parse constant expression
|
||||||
|
// 暂不实现表达式求值
|
||||||
|
LOG_ERROR("Expression in #if/#elif not implemented");
|
||||||
|
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||||
|
return;
|
||||||
case SCC_PP_TOK_LINE:
|
case SCC_PP_TOK_LINE:
|
||||||
case SCC_PP_TOK_EMBED:
|
case SCC_PP_TOK_EMBED:
|
||||||
goto ERROR;
|
goto ERROR;
|
||||||
@@ -297,6 +427,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
|
|||||||
}
|
}
|
||||||
scc_lexer_tok_drop(&tok);
|
scc_lexer_tok_drop(&tok);
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
case SCC_PP_TOK_WARNING:
|
case SCC_PP_TOK_WARNING:
|
||||||
scc_lexer_tok_drop(&tok);
|
scc_lexer_tok_drop(&tok);
|
||||||
while (1) {
|
while (1) {
|
||||||
@@ -309,6 +440,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
|
|||||||
}
|
}
|
||||||
scc_lexer_tok_drop(&tok);
|
scc_lexer_tok_drop(&tok);
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
case SCC_PP_TOK_PRAGMA:
|
case SCC_PP_TOK_PRAGMA:
|
||||||
LOG_WARN("Pragma ignored");
|
LOG_WARN("Pragma ignored");
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ static int switch_file_stack(scc_pproc_t *pp, scc_cstring_t *fname,
|
|||||||
scc_cstring_free(&fpath);
|
scc_cstring_free(&fpath);
|
||||||
scc_cstring_t *syspath = &scc_vec_at(pp->include_paths, i);
|
scc_cstring_t *syspath = &scc_vec_at(pp->include_paths, i);
|
||||||
scc_cstring_append(&fpath, syspath);
|
scc_cstring_append(&fpath, syspath);
|
||||||
|
scc_cstring_append_ch(&fpath, '/');
|
||||||
scc_cstring_append(&fpath, fname);
|
scc_cstring_append(&fpath, fname);
|
||||||
ret = scc_fexists(scc_cstring_as_cstr(&fpath));
|
ret = scc_fexists(scc_cstring_as_cstr(&fpath));
|
||||||
if (ret == true) {
|
if (ret == true) {
|
||||||
@@ -36,7 +37,13 @@ static int switch_file_stack(scc_pproc_t *pp, scc_cstring_t *fname,
|
|||||||
scc_cstring_as_cstr(fname), is_system ? '>' : '\"');
|
scc_cstring_as_cstr(fname), is_system ? '>' : '\"');
|
||||||
return -1;
|
return -1;
|
||||||
FOPEN:
|
FOPEN:
|
||||||
scc_pproc_file_state_t *file = scc_malloc(sizeof(scc_pproc_file_state_t));
|
if (scc_vec_size(pp->file_stack) >= pp->config.max_include_depth) {
|
||||||
|
LOG_FATAL("Include depth is too deep, the include depth is %d, set "
|
||||||
|
"MAX_INCLUDE_DEPTH is %d",
|
||||||
|
scc_vec_size(pp->file_stack), pp->config.max_include_depth);
|
||||||
|
}
|
||||||
|
|
||||||
|
scc_pproc_file_t *file = scc_malloc(sizeof(scc_pproc_file_t));
|
||||||
Assert(file != null);
|
Assert(file != null);
|
||||||
if (scc_sstream_init(&(file->sstream), scc_cstring_as_cstr(&fpath), 1024)) {
|
if (scc_sstream_init(&(file->sstream), scc_cstring_as_cstr(&fpath), 1024)) {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -53,7 +60,7 @@ void scc_pproc_parse_include(scc_pproc_t *pp, scc_lexer_tok_t *include_tok) {
|
|||||||
int ok;
|
int ok;
|
||||||
scc_lexer_tok_t tok;
|
scc_lexer_tok_t tok;
|
||||||
scc_pos_t pos = include_tok->loc;
|
scc_pos_t pos = include_tok->loc;
|
||||||
scc_lexer_tok_drop(&tok);
|
scc_lexer_tok_drop(include_tok);
|
||||||
|
|
||||||
scc_lexer_tok_ring_t *stream = pp->cur_ring;
|
scc_lexer_tok_ring_t *stream = pp->cur_ring;
|
||||||
scc_lexer_tok_vec_t org_toks;
|
scc_lexer_tok_vec_t org_toks;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <pproc_expand.h>
|
#include <pproc_expand.h>
|
||||||
|
#include <scc_lexer_utils.h>
|
||||||
#include <scc_pproc.h>
|
#include <scc_pproc.h>
|
||||||
|
|
||||||
static int pproc_next_one_file(scc_pproc_t *pp, scc_lexer_tok_t *out) {
|
static int pproc_next_one_file(scc_pproc_t *pp, scc_lexer_tok_t *out) {
|
||||||
@@ -25,13 +26,31 @@ CONTINUE:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pp->at_line_start && tok.type == SCC_TOK_SHARP) {
|
if (pp->at_line_start) {
|
||||||
// parse to #
|
if (tok.type == SCC_TOK_SHARP) {
|
||||||
scc_pproc_handle_directive(pp);
|
// parse to #
|
||||||
|
scc_pproc_handle_directive(pp);
|
||||||
|
pp->at_line_start = true;
|
||||||
|
goto CONTINUE;
|
||||||
|
} else if (tok.type == SCC_TOK_BLANK) {
|
||||||
|
scc_ring_next(*stream, *out, ok);
|
||||||
|
scc_ring_peek(*stream, tok, ok);
|
||||||
|
if (ok && tok.type == SCC_TOK_SHARP) {
|
||||||
|
scc_pproc_handle_directive(pp);
|
||||||
|
pp->at_line_start = true;
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
scc_ring_back(*stream, ok);
|
||||||
|
Assert(ok == true);
|
||||||
|
scc_ring_peek(*stream, tok, ok);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pp->enable == false) {
|
||||||
|
scc_lexer_skip_until_newline(stream);
|
||||||
|
pp->at_line_start = true;
|
||||||
goto CONTINUE;
|
goto CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
pp->at_line_start = false;
|
|
||||||
if (tok.type == SCC_TOK_IDENT) {
|
if (tok.type == SCC_TOK_IDENT) {
|
||||||
// maybe expanded
|
// maybe expanded
|
||||||
scc_pproc_macro_t *macro =
|
scc_pproc_macro_t *macro =
|
||||||
@@ -62,7 +81,7 @@ CONTINUE:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
scc_pproc_file_state_t *file = scc_vec_pop(pp->file_stack);
|
scc_pproc_file_t *file = scc_vec_pop(pp->file_stack);
|
||||||
Assert(file->ring == pp->cur_ring);
|
Assert(file->ring == pp->cur_ring);
|
||||||
scc_lexer_drop_ring(file->ring);
|
scc_lexer_drop_ring(file->ring);
|
||||||
scc_lexer_drop(&(file->lexer));
|
scc_lexer_drop(&(file->lexer));
|
||||||
@@ -88,6 +107,9 @@ void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input) {
|
|||||||
scc_vec_init(pp->if_stack);
|
scc_vec_init(pp->if_stack);
|
||||||
scc_vec_init(pp->file_stack);
|
scc_vec_init(pp->file_stack);
|
||||||
pp->at_line_start = true;
|
pp->at_line_start = true;
|
||||||
|
pp->enable = true;
|
||||||
|
|
||||||
|
pp->config.max_include_depth = 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
void scc_pproc_add_builtin_macros() {
|
void scc_pproc_add_builtin_macros() {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ static cbool process_input(const char *input, scc_cstring_t *output) {
|
|||||||
scc_pproc_init(&pp, scc_lexer_to_ring(&lexer, 8, true));
|
scc_pproc_init(&pp, scc_lexer_to_ring(&lexer, 8, true));
|
||||||
|
|
||||||
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pp, 8);
|
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pp, 8);
|
||||||
*output = scc_cstring_create();
|
*output = scc_cstring_from_cstr("");
|
||||||
scc_lexer_tok_t tok;
|
scc_lexer_tok_t tok;
|
||||||
while (1) {
|
while (1) {
|
||||||
scc_ring_next_consume(*tok_ring, tok, ret);
|
scc_ring_next_consume(*tok_ring, tok, ret);
|
||||||
@@ -31,7 +31,6 @@ static cbool process_input(const char *input, scc_cstring_t *output) {
|
|||||||
scc_pproc_drop(&pp);
|
scc_pproc_drop(&pp);
|
||||||
scc_lexer_drop(&lexer);
|
scc_lexer_drop(&lexer);
|
||||||
scc_sstream_drop(&mem_stream);
|
scc_sstream_drop(&mem_stream);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,6 +126,12 @@ static void test_define_nested_macros(void) {
|
|||||||
CHECK_PP_OUTPUT_EXACT(
|
CHECK_PP_OUTPUT_EXACT(
|
||||||
"#define A 1\n#define B (A + 1)\n#define C (B + 1)\nC\n",
|
"#define A 1\n#define B (A + 1)\n#define C (B + 1)\nC\n",
|
||||||
"((1 + 1) + 1)\n");
|
"((1 + 1) + 1)\n");
|
||||||
|
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#define A\n", "");
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#undef A\n", "");
|
||||||
|
|
||||||
|
CHECK_PP_OUTPUT_EXACT(" # define A 1\nA", "1");
|
||||||
|
// CHECK_PP_OUTPUT_EXACT(" # define A 1 \nA", "1"); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_undef_macros(void) {
|
static void test_undef_macros(void) {
|
||||||
@@ -225,6 +230,126 @@ static void test_edge_cases(void) {
|
|||||||
CHECK_PP_OUTPUT_EXACT("#define A B\n#define B C\n#define C 1\nA\n", "1\n");
|
CHECK_PP_OUTPUT_EXACT("#define A B\n#define B C\n#define C 1\nA\n", "1\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_conditional_ifdef(void) {
|
||||||
|
TEST_CASE("ifdef and ifndef");
|
||||||
|
|
||||||
|
// 基本 ifdef / ifndef
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#define FOO\n"
|
||||||
|
"#ifdef FOO\n"
|
||||||
|
"foo\n"
|
||||||
|
"#endif\n",
|
||||||
|
"foo\n");
|
||||||
|
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#define FOO\n"
|
||||||
|
"#ifndef FOO\n"
|
||||||
|
"foo\n"
|
||||||
|
"#endif\n",
|
||||||
|
"");
|
||||||
|
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#undef FOO\n"
|
||||||
|
"#ifdef FOO\n"
|
||||||
|
"foo\n"
|
||||||
|
"#endif\n",
|
||||||
|
"");
|
||||||
|
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#undef FOO\n"
|
||||||
|
"#ifndef FOO\n"
|
||||||
|
"foo\n"
|
||||||
|
"#endif\n",
|
||||||
|
"foo\n");
|
||||||
|
|
||||||
|
// ifdef + else
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#define FOO\n"
|
||||||
|
"#ifdef FOO\n"
|
||||||
|
"foo\n"
|
||||||
|
"#else\n"
|
||||||
|
"bar\n"
|
||||||
|
"#endif\n",
|
||||||
|
"foo\n");
|
||||||
|
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#undef FOO\n"
|
||||||
|
"#ifdef FOO\n"
|
||||||
|
"foo\n"
|
||||||
|
"#else\n"
|
||||||
|
"bar\n"
|
||||||
|
"#endif\n",
|
||||||
|
"bar\n");
|
||||||
|
|
||||||
|
// ifdef + elifdef (C23)
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#define FOO\n"
|
||||||
|
"#ifdef FOO\n"
|
||||||
|
"foo\n"
|
||||||
|
"#elifdef FOO\n"
|
||||||
|
"foo2\n"
|
||||||
|
"#endif\n",
|
||||||
|
"foo\n");
|
||||||
|
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#undef FOO\n"
|
||||||
|
"#define BAR\n"
|
||||||
|
"#ifdef FOO\n"
|
||||||
|
"foo\n"
|
||||||
|
"#elifdef BAR\n"
|
||||||
|
"bar\n"
|
||||||
|
"#else\n"
|
||||||
|
"none\n"
|
||||||
|
"#endif\n",
|
||||||
|
"bar\n");
|
||||||
|
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#undef FOO\n"
|
||||||
|
"#undef BAR\n"
|
||||||
|
"#ifdef FOO\n"
|
||||||
|
"foo\n"
|
||||||
|
"#elifdef BAR\n"
|
||||||
|
"bar\n"
|
||||||
|
"#else\n"
|
||||||
|
"none\n"
|
||||||
|
"#endif\n",
|
||||||
|
"none\n");
|
||||||
|
|
||||||
|
// 嵌套
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#define A\n"
|
||||||
|
"#ifdef A\n"
|
||||||
|
" #ifdef B\n"
|
||||||
|
" inner\n"
|
||||||
|
" #endif\n"
|
||||||
|
" outer\n"
|
||||||
|
"#endif\n",
|
||||||
|
" outer\n");
|
||||||
|
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#define B\n"
|
||||||
|
"#ifndef A\n"
|
||||||
|
" #ifdef B\n"
|
||||||
|
" inner\n"
|
||||||
|
" #endif\n"
|
||||||
|
" outer\n"
|
||||||
|
"#endif\n",
|
||||||
|
" inner\n outer\n");
|
||||||
|
|
||||||
|
// 外层假,内层真
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#ifdef __NONE\n"
|
||||||
|
"#define OUTER\n"
|
||||||
|
"#endif\n"
|
||||||
|
"#ifdef OUTER\n"
|
||||||
|
"should not appear\n"
|
||||||
|
"#endif\n",
|
||||||
|
""); // 期望为空
|
||||||
|
|
||||||
|
// 更复杂的嵌套条件
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#define X\n"
|
||||||
|
"#ifdef X\n"
|
||||||
|
"x defined\n"
|
||||||
|
"#ifdef Y\n"
|
||||||
|
"Y defined\n"
|
||||||
|
"#else\n"
|
||||||
|
"Y not defined\n"
|
||||||
|
"#endif\n"
|
||||||
|
"after inner\n"
|
||||||
|
"#else\n"
|
||||||
|
"X not defined\n"
|
||||||
|
"#endif\n",
|
||||||
|
"x defined\nY not defined\nafter inner\n");
|
||||||
|
}
|
||||||
|
|
||||||
#define TEST_LIST_CASE(func_name) {#func_name, func_name}
|
#define TEST_LIST_CASE(func_name) {#func_name, func_name}
|
||||||
TEST_LIST = {
|
TEST_LIST = {
|
||||||
TEST_LIST_CASE(test_define_simple_no_macro),
|
TEST_LIST_CASE(test_define_simple_no_macro),
|
||||||
@@ -237,5 +362,6 @@ TEST_LIST = {
|
|||||||
TEST_LIST_CASE(test_define_nested_macros),
|
TEST_LIST_CASE(test_define_nested_macros),
|
||||||
TEST_LIST_CASE(test_undef_macros),
|
TEST_LIST_CASE(test_undef_macros),
|
||||||
TEST_LIST_CASE(hard_test_define_func_macros),
|
TEST_LIST_CASE(hard_test_define_func_macros),
|
||||||
|
TEST_LIST_CASE(test_conditional_ifdef),
|
||||||
{NULL, NULL},
|
{NULL, NULL},
|
||||||
};
|
};
|
||||||
@@ -7,7 +7,12 @@
|
|||||||
#define __SCC_LOG_IMPL_H__
|
#define __SCC_LOG_IMPL_H__
|
||||||
|
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
|
#ifndef __SCC__
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#else
|
||||||
|
// TODO
|
||||||
|
#warning "TODO: implement stdarg.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __SCC_LOG_IMPL_USE_STD_IMPL__
|
#ifdef __SCC_LOG_IMPL_USE_STD_IMPL__
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -21,7 +26,7 @@
|
|||||||
#define __smcc_log_unreachable() (__builtin_unreachable())
|
#define __smcc_log_unreachable() (__builtin_unreachable())
|
||||||
#elif defined _MSC_VER // MSVC
|
#elif defined _MSC_VER // MSVC
|
||||||
#define __smcc_log_unreachable() (__assume(false))
|
#define __smcc_log_unreachable() (__assume(false))
|
||||||
#elif defined __SMCC_BUILT_IN__ // The SMCC (my compiler)
|
#elif defined __SCC_BUILT_IN__ // The SCC Compiler (my compiler)
|
||||||
#define __smcc_log_unreachable() (__smcc_builtin_unreachable())
|
#define __smcc_log_unreachable() (__smcc_builtin_unreachable())
|
||||||
#else
|
#else
|
||||||
#define __smcc_log_unreachable()
|
#define __smcc_log_unreachable()
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
#ifndef __SCC_CORE_TYPE_H__
|
#ifndef __SCC_CORE_TYPE_H__
|
||||||
#define __SCC_CORE_TYPE_H__
|
#define __SCC_CORE_TYPE_H__
|
||||||
|
|
||||||
#ifndef __SCC_BUILTIN_TYPE__
|
#ifndef __SCC__
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@@ -48,6 +49,29 @@ static_assert(sizeof(cbool) == 1, "cbool size must 1");
|
|||||||
#define __scc_null
|
#define __scc_null
|
||||||
#define __scc_isize
|
#define __scc_isize
|
||||||
#define __scc_usize
|
#define __scc_usize
|
||||||
|
|
||||||
|
/* clang-format off */
|
||||||
|
typedef __scc_i8 i8;
|
||||||
|
typedef __scc_i16 i16;
|
||||||
|
typedef __scc_i32 i32;
|
||||||
|
typedef __scc_i64 i64;
|
||||||
|
typedef __scc_u8 u8;
|
||||||
|
typedef __scc_u16 u16;
|
||||||
|
typedef __scc_u32 u32;
|
||||||
|
typedef __scc_u64 u64;
|
||||||
|
|
||||||
|
typedef __scc_isize isize;
|
||||||
|
typedef __scc_usize usize;
|
||||||
|
typedef __scc_isize pdiff;
|
||||||
|
|
||||||
|
typedef __scc_f32 f32;
|
||||||
|
typedef __scc_f64 f64;
|
||||||
|
|
||||||
|
typedef __scc_bool cbool;
|
||||||
|
/// void / null
|
||||||
|
#define null __scc_null
|
||||||
|
|
||||||
|
/* clang-format on */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef union scc_cvalue {
|
typedef union scc_cvalue {
|
||||||
|
|||||||
17
src/main.c
17
src/main.c
@@ -11,6 +11,7 @@ typedef struct {
|
|||||||
const char *input_file;
|
const char *input_file;
|
||||||
const char *output_file;
|
const char *output_file;
|
||||||
int verbose;
|
int verbose;
|
||||||
|
scc_argparse_list_t include_paths;
|
||||||
cbool emit_lex;
|
cbool emit_lex;
|
||||||
cbool emit_pp;
|
cbool emit_pp;
|
||||||
cbool emit_ast;
|
cbool emit_ast;
|
||||||
@@ -89,9 +90,12 @@ static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
|
|||||||
scc_argparse_spec_set_required(&arg_input.spec, true);
|
scc_argparse_spec_set_required(&arg_input.spec, true);
|
||||||
scc_argparse_cmd_add_arg(root, &arg_input);
|
scc_argparse_cmd_add_arg(root, &arg_input);
|
||||||
|
|
||||||
|
// -I, --include (添加额外的系统头文件搜索路径)
|
||||||
scc_argparse_opt_t opt_include;
|
scc_argparse_opt_t opt_include;
|
||||||
scc_argparse_opt_init(&opt_include, 0, "include",
|
scc_argparse_opt_init(&opt_include, 'I', "include",
|
||||||
scc_hints[SCC_HINT_INCLUDE_PATH]);
|
scc_hints[SCC_HINT_INCLUDE_PATH]);
|
||||||
|
scc_argparse_spec_setup_list(&opt_include.spec, &(config->include_paths));
|
||||||
|
scc_argparse_cmd_add_opt(root, &opt_include);
|
||||||
|
|
||||||
// -v, --verbose (计数)
|
// -v, --verbose (计数)
|
||||||
scc_argparse_opt_t opt_verbose;
|
scc_argparse_opt_t opt_verbose;
|
||||||
@@ -194,6 +198,8 @@ int main(int argc, const char **argv, const char **envp) {
|
|||||||
.emit_ast = false,
|
.emit_ast = false,
|
||||||
.emit_ir = false,
|
.emit_ir = false,
|
||||||
};
|
};
|
||||||
|
scc_vec_init(config.include_paths);
|
||||||
|
|
||||||
scc_argparse_t argparse;
|
scc_argparse_t argparse;
|
||||||
setup_argparse(&argparse, &config, SCC_ARGPARSE_LANG_ZH);
|
setup_argparse(&argparse, &config, SCC_ARGPARSE_LANG_ZH);
|
||||||
int ret = scc_argparse_parse(&argparse, argc, argv);
|
int ret = scc_argparse_parse(&argparse, argc, argv);
|
||||||
@@ -223,6 +229,15 @@ int main(int argc, const char **argv, const char **envp) {
|
|||||||
|
|
||||||
scc_pproc_t pproc;
|
scc_pproc_t pproc;
|
||||||
scc_pproc_init(&pproc, scc_lexer_to_ring(&lexer, 8, true));
|
scc_pproc_init(&pproc, scc_lexer_to_ring(&lexer, 8, true));
|
||||||
|
scc_vec_foreach(config.include_paths, i) {
|
||||||
|
scc_pproc_add_include_path_cstr(&pproc,
|
||||||
|
scc_vec_at(config.include_paths, i));
|
||||||
|
}
|
||||||
|
scc_lexer_tok_vec_t pproc_tok_vec;
|
||||||
|
scc_vec_init(pproc_tok_vec);
|
||||||
|
scc_cstring_t pproc_macro_name = scc_cstring_from_cstr("__SCC__");
|
||||||
|
scc_pproc_add_object_macro(&(pproc.macro_table), &pproc_macro_name,
|
||||||
|
&pproc_tok_vec);
|
||||||
if (config.emit_pp) {
|
if (config.emit_pp) {
|
||||||
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pproc, 8);
|
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pproc, 8);
|
||||||
if (config.output_file == null) {
|
if (config.output_file == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user