refactor(log): 统一日志系统并添加链接器实现

- 为所有模块添加统一的 scc_*_log.h 日志头文件,删除旧的 lexer_log.h/c
- 将运行时日志从 scc_utils 迁移到 scc_core 目录,统一日志管理
- 在解析器表达式/语句/类型解析中添加 LOG_TRACE 调试日志
- 实现 SCCF 链接器 (sccf_linker) 支持多目标文件链接
- 重构 CLI 主程序 (main.c/config.c),新增 cmd_log.h 调试支持
- 优化 x86 指令编码操作数对齐检查
- 修复预处理器的指令处理和宏展开逻辑
This commit is contained in:
zzy
2026-06-05 13:07:41 +08:00
parent 0acea43e4e
commit 88846d7479
70 changed files with 1497 additions and 469 deletions

View File

@@ -0,0 +1,26 @@
#ifndef __SCC_PPROC_LOG_H__
#define __SCC_PPROC_LOG_H__
typedef struct logger logger_t;
extern logger_t __scc_pproc_log;
extern logger_t __scc_pproc_user;
#define SCC_LOG_HANDLER &__scc_pproc_user
#define LOG_DEFAULT_HANDLER &__scc_pproc_log
#include <scc_log.h>
#ifdef __SCC_PPROC_LOG_IMPL__
logger_t __scc_pproc_log = {
.name = "pproc",
.level = LOG_LEVEL_ALL,
.handler = log_default_handler,
};
logger_t __scc_pproc_user = {
.name = "pproc",
.level = LOG_LEVEL_ALL,
.user_handler = scc_log_handler,
};
#endif
#endif

View File

@@ -1,3 +1,5 @@
#include <scc_pproc_log.h>
#include <pproc_expand.h>
#include <scc_core_ring.h>
#include <scc_lexer_utils.h>
@@ -353,7 +355,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
goto ERROR;
case SCC_PP_TOK_EMBED:
goto ERROR;
#define __SCC_PPROC_LOG_PNT(func_name) \
#define __SCC_PPROC_LOG_ERR() \
do { \
scc_lexer_tok_drop(&tok); \
while (1) { \
@@ -362,17 +364,33 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
return; \
} \
if (scc_get_tok_subtype(tok.type) == SCC_TOK_SUBTYPE_LITERAL) { \
func_name(tok.loc, "%s", scc_str_as_cstr(&tok.lexeme)); \
SCC_ERROR(tok.loc, "%s", scc_str_as_cstr(&tok.lexeme)); \
} \
scc_lexer_tok_drop(&tok); \
} \
return; \
} while (0)
#define __SCC_PPROC_LOG_WARN() \
do { \
scc_lexer_tok_drop(&tok); \
while (1) { \
ok = scc_lexer_next_non_blank(pp->cur_ring, &tok); \
if (tok.type == SCC_TOK_ENDLINE || ok == false) { \
return; \
} \
if (scc_get_tok_subtype(tok.type) == SCC_TOK_SUBTYPE_LITERAL) { \
SCC_WARN(tok.loc, "%s", scc_str_as_cstr(&tok.lexeme)); \
} \
scc_lexer_tok_drop(&tok); \
} \
return; \
} while (0)
case SCC_PP_TOK_ERROR: {
__SCC_PPROC_LOG_PNT(SCC_ERROR);
__SCC_PPROC_LOG_ERR();
}
case SCC_PP_TOK_WARNING: {
__SCC_PPROC_LOG_PNT(SCC_WARN);
__SCC_PPROC_LOG_WARN();
}
case SCC_PP_TOK_PRAGMA:
SCC_WARN(tok.loc, "pragma ignored");

View File

@@ -1,3 +1,5 @@
#include <scc_pproc_log.h>
#include <pproc_expand.h>
#include <scc_lexer_utils.h>
#include <scc_pproc.h>
@@ -125,6 +127,8 @@ void scc_pproc_expand_by_src(scc_pproc_macro_table_t *macro_table,
scc_lexer_tok_ring_t *output,
const scc_pproc_macro_t *macro,
cbool need_keep_org_pos) {
LOG_DEBUG("expand macro '%s' type=%s", scc_str_as_cstr(&macro->name),
macro->type == SCC_PP_MACRO_OBJECT ? "object" : "function");
scc_lexer_tok_vec_t expaned_buffer;
scc_vec_init(expaned_buffer);
@@ -412,6 +416,8 @@ static void concact(scc_pproc_expand_t *ctx, scc_lexer_tok_vec_t *tok_buffer,
static inline void expand_function_macro(scc_pproc_expand_t *ctx,
const scc_pproc_macro_t *macro) {
LOG_DEBUG("expand function macro '%s' (line %d)",
scc_str_as_cstr(&macro->name), ctx->call_pos.line);
scc_lexer_tok_vec_t tok_buffer;
scc_vec_init(tok_buffer);
@@ -543,6 +549,7 @@ static inline void expand_function_macro(scc_pproc_expand_t *ctx,
static inline void expand_object_macro(scc_pproc_expand_t *ctx,
const scc_pproc_macro_t *macro) {
LOG_DEBUG("expand object macro '%s'", scc_str_as_cstr(&macro->name));
scc_lexer_tok_vec_t tok_buffer;
scc_vec_init(tok_buffer);
@@ -578,13 +585,13 @@ static inline void expand_object_macro(scc_pproc_expand_t *ctx,
static cbool parse_defined(scc_pproc_expand_t *expand_ctx, scc_pos_t *tok_pos) {
scc_lexer_tok_t next_tok = {0};
if (scc_lexer_next_non_blank(expand_ctx->input, &next_tok) == false) {
SCC_ERROR(*tok_pos, "Unexpected before defined EOF");
SCC_ERROR(*tok_pos, "unexpected before 'defined' EOF");
}
if (next_tok.type == SCC_TOK_L_PAREN) {
scc_lexer_tok_drop(&next_tok);
scc_lexer_next_non_blank(expand_ctx->input, &next_tok);
if (scc_get_tok_subtype(next_tok.type) != SCC_TOK_SUBTYPE_IDENTIFIER) {
SCC_ERROR(next_tok.loc, "Expected identifier before defined");
SCC_ERROR(next_tok.loc, "expected identifier before 'defined'");
scc_lexer_tok_drop(&next_tok);
}
@@ -603,7 +610,7 @@ static cbool parse_defined(scc_pproc_expand_t *expand_ctx, scc_pos_t *tok_pos) {
scc_lexer_tok_drop(&next_tok);
return false;
} else {
SCC_ERROR(next_tok.loc, "Expected ')'");
SCC_ERROR(next_tok.loc, "expected ')'");
scc_lexer_tok_drop(&next_tok);
}
}
@@ -652,10 +659,14 @@ void scc_pproc_expand_macro(scc_pproc_expand_t *expand_ctx) {
if (macro == nullptr || need_skip(expand_ctx, macro)) {
// FIXME maybe keyword is error or don't parse c keyword or number
LOG_DEBUG("skip macro expansion for '%s'",
scc_str_as_cstr(&tok.lexeme));
tok.type += SCC_TOK_DISABLED;
scc_vec_push(expand_ctx->output, tok);
continue;
}
LOG_DEBUG("expand macro '%s' type=%s", scc_str_as_cstr(&tok.lexeme),
macro->type == SCC_PP_MACRO_OBJECT ? "object" : "function");
expand_ctx->need_rescan = true;
expand_ctx->call_pos = tok.loc;

View File

@@ -1,3 +1,5 @@
#include <scc_pproc_log.h>
#include <scc_lexer_utils.h>
#include <scc_pproc.h>

View File

@@ -1,3 +1,5 @@
#include <scc_pproc_log.h>
#include <pproc_expand.h>
#include <scc_core_ring.h>
#include <scc_path.h>
@@ -39,9 +41,8 @@ static int switch_file_stack(scc_pproc_t *pp, scc_str_t *fname, scc_pos_t *pos,
return -1;
FOPEN:
if ((int)scc_vec_size(pp->file_stack) >= pp->config.max_include_depth) {
SCC_FATAL(*pos, "include depth exceeds maximum (%d)",
LOG_FATAL("Include depth exceeds maximum (%d)",
pp->config.max_include_depth);
LOG_FATAL("Include depth is too deep...");
}
scc_pproc_file_t *file = scc_malloc(sizeof(scc_pproc_file_t));

View File

@@ -1,3 +1,5 @@
#include <scc_pproc_log.h>
#include <pproc_macro.h>
// 创建宏对象

View File

@@ -1,3 +1,6 @@
#define __SCC_PPROC_LOG_IMPL__
#include <scc_pproc_log.h>
#include <pproc_expand.h>
#include <scc_lexer.h>
#include <scc_lexer_utils.h>

View File

@@ -626,6 +626,11 @@ static void test_c99_docs(void) {
"fprintf(stderr, \"X = %d\\n\", x);\n"
"puts(\"The first, second, and third items.\");\n"
"((x>y)?puts(\"x>y\"): printf(\"x is %d but y is %d\", x, y));\n");
TEST_CASE("Typedef");
CHECK_PP_OUTPUT_EXACT(
"#define SCC_AP_DIGIT u64\ntypedef SCC_AP_DIGIT scc_ap_digit;",
"typedef u64 scc_ap_digit;");
}
#define TEST_LIST_CASE(func_name) {#func_name, func_name}