refactor(argparse): 将null替换为nullptr以提高C++兼容性

- 在argparse库中将所有null指针常量替换为nullptr
- 更新头文件和源文件中的指针初始化和比较操作
- 修改测试文件中的相关断言检查
- 更新AST定义文件中的注释说明
This commit is contained in:
zzy
2026-04-05 20:18:09 +08:00
parent 27d86d5685
commit 4144f7841c
76 changed files with 1430 additions and 998 deletions

View File

@@ -173,10 +173,10 @@ scc_ast_expr_t *scc_parse_initializer(scc_parser_t *parser,
[ constant-expression ]
. identifier
*/
const scc_lexer_tok_t *tok_ptr = null;
const scc_lexer_tok_t *tok_ptr = nullptr;
scc_lexer_tok_t tok = {0};
tok_ptr = scc_parser_peek(parser);
scc_ast_expr_t *init = null;
scc_ast_expr_t *init = nullptr;
if (!(tok_ptr && tok_ptr->type == SCC_TOK_L_BRACE)) {
// TODO int a = 1, b = 1;
init = scc_parse_assignment_expression(parser);
@@ -187,23 +187,23 @@ scc_ast_expr_t *scc_parse_initializer(scc_parser_t *parser,
scc_lexer_tok_drop(&tok);
init = scc_malloc(sizeof(scc_ast_expr_t));
Assert(init != null);
Assert(init != nullptr);
scc_ast_expr_vec_t lhs_exprs;
scc_vec_init(lhs_exprs);
scc_ast_expr_vec_t rhs_exprs;
scc_vec_init(rhs_exprs);
scc_ast_expr_t *lhs = null;
scc_ast_expr_t *rhs = null;
scc_ast_expr_t *lhs = nullptr;
scc_ast_expr_t *rhs = nullptr;
scc_ast_expr_t *ptr = base;
while (1) {
tok_ptr = scc_parser_peek(parser);
if (tok_ptr->type == SCC_TOK_DOT) {
scc_parser_next_consume(parser, null);
scc_parser_next_consume(parser, nullptr);
tok_ptr = scc_parser_peek(parser);
if (tok_ptr && tok_ptr->type == SCC_TOK_IDENT) {
scc_parser_next_consume(parser, &tok);
lhs = scc_malloc(sizeof(scc_ast_expr_t));
Assert(lhs != null);
Assert(lhs != nullptr);
scc_ast_expr_member_init(lhs, ptr, scc_str_as_cstr(&tok.lexeme),
tok.loc);
if (!scc_parser_consume_if(parser, SCC_TOK_ASSIGN)) {
@@ -211,7 +211,7 @@ scc_ast_expr_t *scc_parse_initializer(scc_parser_t *parser,
continue;
}
rhs = scc_parse_initializer(parser, lhs);
if (rhs == null) {
if (rhs == nullptr) {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Expected initializer");
Panic();
@@ -224,34 +224,34 @@ scc_ast_expr_t *scc_parse_initializer(scc_parser_t *parser,
"Expected 'identifier' after '.'");
}
} else if (tok_ptr->type == SCC_TOK_L_BRACKET) {
scc_parser_next_consume(parser, null);
scc_parser_next_consume(parser, nullptr);
scc_ast_expr_t *idx = scc_parser_constant_expression(parser);
Assert(idx != null);
Assert(idx != nullptr);
if (!scc_parser_consume_if(parser, SCC_TOK_R_BRACKET)) {
SCC_ERROR(scc_parser_got_current_pos(parser), "Expected ']'");
}
lhs = scc_malloc(sizeof(scc_ast_expr_t));
Assert(lhs != null);
Assert(lhs != nullptr);
scc_ast_expr_array_subscript_init(lhs, ptr, idx, tok_ptr->loc);
if (!scc_parser_consume_if(parser, SCC_TOK_ASSIGN)) {
ptr = lhs;
continue;
}
rhs = scc_parse_initializer(parser, lhs);
Assert(rhs != null);
Assert(rhs != nullptr);
scc_vec_push(lhs_exprs, lhs);
scc_vec_push(rhs_exprs, rhs);
ptr = base;
} else if (tok_ptr->type == SCC_TOK_R_BRACE) {
scc_parser_next_consume(parser, null);
scc_parser_next_consume(parser, nullptr);
break;
} else if (tok_ptr->type == SCC_TOK_COMMA) {
scc_parser_next_consume(parser, null);
scc_parser_next_consume(parser, nullptr);
continue;
} else {
// FIXME
scc_ast_expr_t *expr = scc_parse_initializer(parser, base);
scc_vec_push(lhs_exprs, null);
scc_vec_push(lhs_exprs, nullptr);
scc_vec_push(rhs_exprs, expr);
}
}
@@ -260,29 +260,29 @@ scc_ast_expr_t *scc_parse_initializer(scc_parser_t *parser,
}
scc_ast_decl_t *scc_parse_declaration(scc_parser_t *parser) {
const scc_lexer_tok_t *tok_ptr = null;
const scc_lexer_tok_t *tok_ptr = nullptr;
scc_ast_decl_t *decl_list = null;
scc_ast_decl_t *decl_list = nullptr;
scc_ast_decl_vec_t decl_list_vec;
scc_vec_init(decl_list_vec);
scc_ast_type_t *type = scc_parse_declaration_specifiers(parser);
if (type == null) {
return null;
if (type == nullptr) {
return nullptr;
}
scc_ast_decl_specifier_t spec = type->quals;
// FIXME drop typedef inline and ...
type->quals.is_typedef = false;
scc_ast_decl_t *decl = null;
scc_ast_decl_t *decl = nullptr;
CONTINUE:
decl = scc_parse_declarator(parser, type);
if (decl == null) {
return null;
if (decl == nullptr) {
return nullptr;
}
tok_ptr = scc_parser_peek(parser);
if (tok_ptr->type == SCC_TOK_ASSIGN) {
scc_parser_next_consume(parser, null);
scc_parser_next_consume(parser, nullptr);
// TODO maybe memory leak
scc_ast_expr_t *lvalue = scc_malloc(sizeof(scc_ast_expr_t));
scc_ast_expr_lvalue_init(lvalue, decl->var.type, decl->base.loc);
@@ -290,7 +290,7 @@ CONTINUE:
} else if (tok_ptr->type == SCC_TOK_L_BRACE) {
scc_parse_decl_sema(parser, decl);
parser->sema_callbacks.on_decl(parser->sema_callbacks.context,
scc_ast_decl_t_BEGIN, null);
scc_ast_decl_t_BEGIN, nullptr);
scc_vec_foreach(decl->func.type->function.params, i) {
scc_ast_decl_t *param =
scc_vec_at(decl->func.type->function.params, i);
@@ -299,19 +299,19 @@ CONTINUE:
}
scc_ast_stmt_t *body = scc_parse_statement(parser);
parser->sema_callbacks.on_decl(parser->sema_callbacks.context,
scc_ast_decl_t_END, null);
scc_ast_decl_t_END, nullptr);
Assert(decl->base.type == SCC_AST_DECL_FUNC);
decl->func.body = body;
Assert(decl->func.type != null);
Assert(decl->func.type != nullptr);
Assert(decl->func.type->base.type == SCC_AST_TYPE_FUNCTION);
Assert(decl->func.body != null);
Assert(decl->func.body != nullptr);
Assert(decl->func.body->base.type == SCC_AST_STMT_COMPOUND);
goto RETURN;
}
tok_ptr = scc_parser_peek(parser);
if (tok_ptr->type == SCC_TOK_SEMICOLON) {
scc_parser_next_consume(parser, null);
scc_parser_next_consume(parser, nullptr);
if (decl_list)
scc_vec_push(decl_list_vec, decl);
if (spec.is_typedef) {
@@ -326,7 +326,7 @@ CONTINUE:
decl->base.loc);
}
}
if (decl_list != null) {
if (decl_list != nullptr) {
scc_vec_foreach(decl_list_vec, i) {
decl = scc_vec_at(decl_list_vec, i);
scc_parse_decl_sema(parser, decl);
@@ -340,10 +340,10 @@ CONTINUE:
}
goto RETURN;
} else if (tok_ptr->type == SCC_TOK_COMMA) {
scc_parser_next_consume(parser, null);
if (decl_list == null) {
scc_parser_next_consume(parser, nullptr);
if (decl_list == nullptr) {
decl_list = scc_malloc(sizeof(scc_ast_decl_t));
Assert(decl_list != null);
Assert(decl_list != nullptr);
scc_vec_push(decl_list_vec, decl);
} else {
Assert(scc_vec_size(decl_list_vec) != 0);
@@ -361,5 +361,5 @@ CONTINUE:
RETURN:
return decl;
ERROR:
return null;
return nullptr;
}