feat(pproc): 改进宏处理器中的标识符识别逻辑
修复了宏处理器中对标识符类型的检查方式,使用更准确的子类型判断函数, 确保关键字也能被正确处理为宏名或参数名。同时添加了相关的单元测试用例。 BREAKING CHANGE: 修改了token类型的检查方法
This commit is contained in:
@@ -1,9 +1,22 @@
|
|||||||
|
|
||||||
lexer 词法分析
|
lexer 词法分析
|
||||||
parse 语法分析
|
|
||||||
|
pproc 预处理器
|
||||||
|
|
||||||
|
parser 语法分析 sema 语义分析
|
||||||
|
|
||||||
ast 抽象语法树
|
ast 抽象语法树
|
||||||
sema 语义分析
|
|
||||||
ir 中间代码标识
|
ir 中间代码标识
|
||||||
|
|
||||||
|
- ast2ir
|
||||||
|
|
||||||
|
- ir2mcode
|
||||||
|
|
||||||
opt 优化器
|
opt 优化器
|
||||||
codegen 代码生成
|
|
||||||
|
mcode 机器码
|
||||||
|
|
||||||
|
sccf 统一输出格式
|
||||||
|
|
||||||
target 目标平台支持
|
target 目标平台支持
|
||||||
|
|||||||
@@ -119,7 +119,8 @@ void scc_pproc_parse_function_macro(scc_pproc_t *pp,
|
|||||||
if (idx++ % 2 != 1) {
|
if (idx++ % 2 != 1) {
|
||||||
LOG_FATAL("ERROR");
|
LOG_FATAL("ERROR");
|
||||||
}
|
}
|
||||||
} else if (arg->type == SCC_TOK_IDENT) {
|
} else if (scc_get_tok_subtype(arg->type) ==
|
||||||
|
SCC_TOK_SUBTYPE_IDENTIFIER) {
|
||||||
if (idx++ % 2 != 0) {
|
if (idx++ % 2 != 0) {
|
||||||
LOG_FATAL("ERROR");
|
LOG_FATAL("ERROR");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ CONTINUE:
|
|||||||
scc_ring_next_consume(*stream, *out, ok);
|
scc_ring_next_consume(*stream, *out, ok);
|
||||||
pp->at_line_start = true;
|
pp->at_line_start = true;
|
||||||
return true;
|
return true;
|
||||||
} else if (tok.type == SCC_TOK_IDENT) {
|
} else if (scc_get_tok_subtype(tok.type) == SCC_TOK_SUBTYPE_IDENTIFIER) {
|
||||||
// maybe expanded
|
// maybe expanded
|
||||||
scc_pproc_macro_t *macro =
|
scc_pproc_macro_t *macro =
|
||||||
scc_pproc_macro_table_get(&pp->macro_table, &tok.lexeme);
|
scc_pproc_macro_table_get(&pp->macro_table, &tok.lexeme);
|
||||||
|
|||||||
@@ -181,6 +181,19 @@ static void test_define_nested_macros(void) {
|
|||||||
"#define x 2\n"
|
"#define x 2\n"
|
||||||
"g(z)\n",
|
"g(z)\n",
|
||||||
"f(2 * (z))\n");
|
"f(2 * (z))\n");
|
||||||
|
|
||||||
|
TEST_CASE("with keyword as macro name or parameter name");
|
||||||
|
CHECK_PP_OUTPUT_EXACT(
|
||||||
|
"#define ASSIGN_PTR_OR_DEFAULT(assigned_val, value, default)\\\n"
|
||||||
|
"assigned_val = value ? value : default\n"
|
||||||
|
"ASSIGN_PTR_OR_DEFAULT(parser->sema_callbacks.on_decl,"
|
||||||
|
"callbacks->on_decl, dummy_sema_callback);\n",
|
||||||
|
"parser->sema_callbacks.on_decl = callbacks->on_decl ? "
|
||||||
|
"callbacks->on_decl : dummy_sema_callback;\n");
|
||||||
|
|
||||||
|
CHECK_PP_OUTPUT_EXACT("#define default a\n"
|
||||||
|
"default\n",
|
||||||
|
"a\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_undef_macros(void) {
|
static void test_undef_macros(void) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// IMPLEMENT
|
// IMPLEMENT
|
||||||
#include <printf/printf.c>
|
#include <scc_printf/printf.c>
|
||||||
|
|
||||||
#include <scc_core_impl.h>
|
#include <scc_core_impl.h>
|
||||||
#define __SCC_LOG_IMPL_IMPORT_SRC__
|
#define __SCC_LOG_IMPL_IMPORT_SRC__
|
||||||
@@ -16,15 +16,7 @@ scc_file_t scc_fopen(const char *path, scc_fmode_t mode) {
|
|||||||
|
|
||||||
void scc_fclose(scc_file_t file) { scc_pal_fclose(file); }
|
void scc_fclose(scc_file_t file) { scc_pal_fclose(file); }
|
||||||
|
|
||||||
usize scc_fread(scc_file_t file, void *buffer, usize size) {
|
usize scc_fsize(scc_file_t file) {
|
||||||
return scc_pal_fread(file, buffer, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
usize scc_fwrite(scc_file_t file, const void *buffer, usize size) {
|
|
||||||
return scc_pal_fwrite(file, buffer, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
usize scc_fsize(scc_pal_file_t file) {
|
|
||||||
if (scc_pal_fseek(file, 0, SCC_SEEK_PAL_END) != 0) {
|
if (scc_pal_fseek(file, 0, SCC_SEEK_PAL_END) != 0) {
|
||||||
LOG_ERROR("fseek failed");
|
LOG_ERROR("fseek failed");
|
||||||
return 0;
|
return 0;
|
||||||
@@ -37,6 +29,14 @@ usize scc_fsize(scc_pal_file_t file) {
|
|||||||
return fsize;
|
return fsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
usize scc_fread(scc_file_t file, void *buffer, usize size) {
|
||||||
|
return scc_pal_fread(file, buffer, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
usize scc_fwrite(scc_file_t file, const void *buffer, usize size) {
|
||||||
|
return scc_pal_fwrite(file, buffer, size);
|
||||||
|
}
|
||||||
|
|
||||||
cbool scc_fexists(const char *path) {
|
cbool scc_fexists(const char *path) {
|
||||||
scc_file_t fp = scc_fopen(path, SCC_FILE_READ);
|
scc_file_t fp = scc_fopen(path, SCC_FILE_READ);
|
||||||
if (!fp)
|
if (!fp)
|
||||||
|
|||||||
Reference in New Issue
Block a user