harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

commit 10543441a8d95e78299fbc77ceb56fd9b61d0baf
parent e7de13d7e365d5b7266f3e80b114a539266d76d2
Author: Sebastian <sebastian@sebsite.pw>
Date:   Tue, 17 Jan 2023 01:05:35 -0500

util: add xstrdup

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Minclude/util.h | 2++
Msrc/check.c | 12++++++------
Msrc/gen.c | 16++++++++--------
Msrc/genutil.c | 4++--
Msrc/identifier.c | 6+++---
Msrc/lex.c | 2+-
Msrc/mod.c | 4++--
Msrc/parse.c | 4++--
Msrc/qbe.c | 2+-
Msrc/type_store.c | 4++--
Msrc/util.c | 11+++++++++++
11 files changed, 40 insertions(+), 27 deletions(-)

diff --git a/include/util.h b/include/util.h @@ -14,10 +14,12 @@ uint32_t fnv1a_size(uint32_t hash, size_t sz); uint32_t fnv1a_s(uint32_t hash, const char *str); void *xcalloc(size_t n, size_t s); void *xrealloc(void *p, size_t s); +char *xstrdup(const char *s); #define malloc(a) (void *)sizeof(struct { static_assert(0, "Use xcalloc instead"); int _; }) #define calloc(a, b) (void *)sizeof(struct { static_assert(0, "Use xcalloc instead"); int _; }) #define realloc(a, b) (void *)sizeof(struct { static_assert(0, "Use xrealloc instead"); int _; }) +#define strdup(s) (char *)(sizeof(struct { static_assert(0, "Use xstrdup instead"); int _; }) struct pathspec { const char *var; diff --git a/src/check.c b/src/check.c @@ -21,7 +21,7 @@ mkident(struct context *ctx, struct identifier *out, const struct identifier *in const char *symbol) { if (symbol) { - out->name = strdup(symbol); + out->name = xstrdup(symbol); return; } identifier_dup(out, in); @@ -1648,8 +1648,8 @@ check_expr_compound(struct context *ctx, expr->compound.scope = scope; if (aexpr->compound.label) { - expr->compound.label = strdup(aexpr->compound.label); - scope->label = strdup(aexpr->compound.label); + expr->compound.label = xstrdup(aexpr->compound.label); + scope->label = xstrdup(aexpr->compound.label); } struct expressions *list = &expr->compound.exprs; @@ -3339,7 +3339,7 @@ check_function(struct context *ctx, decl->func.flags = afndecl->flags; if (afndecl->symbol) { - decl->symbol = strdup(afndecl->symbol); + decl->symbol = xstrdup(afndecl->symbol); } mkident(ctx, &decl->ident, &afndecl->ident, NULL); @@ -3436,7 +3436,7 @@ check_global(struct context *ctx, decl->global.threadlocal = adecl->threadlocal; if (adecl->symbol) { - decl->symbol = strdup(adecl->symbol); + decl->symbol = xstrdup(adecl->symbol); } mkident(ctx, &decl->ident, &adecl->ident, NULL); @@ -3937,7 +3937,7 @@ scan_enum_field_aliases(struct context *ctx, const struct scope_object *obj) xcalloc(1, sizeof(struct ast_enum_field)); *afield = (struct ast_enum_field){ .loc = (struct location){0}, // XXX: what to put here? - .name = strdup(val->name.name), + .name = xstrdup(val->name.name), }; struct incomplete_enum_field *field = diff --git a/src/gen.c b/src/gen.c @@ -3302,7 +3302,7 @@ gen_function_decl(struct gen_context *ctx, const struct declaration *decl) } else if (func->flags & FN_FINI) { qdef->name = gen_name(ctx, "finifunc.%d"); } else { - qdef->name = decl->symbol ? strdup(decl->symbol) + qdef->name = decl->symbol ? xstrdup(decl->symbol) : ident_to_sym(&decl->ident); } @@ -3326,7 +3326,7 @@ gen_function_decl(struct gen_context *ctx, const struct declaration *decl) const struct type *type = obj->type; param = *next = xcalloc(1, sizeof(struct qbe_func_param)); assert(!obj->ident.ns); // Invariant - param->name = strdup(obj->ident.name); + param->name = xstrdup(obj->ident.name); param->type = qtype_lookup(ctx, type, false); struct gen_binding *gb = @@ -3336,7 +3336,7 @@ gen_function_decl(struct gen_context *ctx, const struct declaration *decl) gb->object = obj; if (type_is_aggregate(type)) { // No need to copy to stack - gb->value.name = strdup(param->name); + gb->value.name = xstrdup(param->name); } else { gb->value.name = gen_name(ctx, "param.%d"); @@ -3393,7 +3393,7 @@ gen_function_decl(struct gen_context *ctx, const struct declaration *decl) .value = { .kind = QV_GLOBAL, .type = &qbe_long, - .name = strdup(qdef->name), + .name = xstrdup(qdef->name), }, .next = NULL, }; @@ -3419,7 +3419,7 @@ gen_function_decl(struct gen_context *ctx, const struct declaration *decl) .value = { .kind = QV_GLOBAL, .type = &qbe_long, - .name = strdup(qdef->name), + .name = xstrdup(qdef->name), }, .next = NULL, }; @@ -3460,7 +3460,7 @@ gen_function_decl(struct gen_context *ctx, const struct declaration *decl) next->type = QD_VALUE; next->value.kind = QV_GLOBAL; next->value.type = &qbe_long; - next->value.name = strdup(qdef->name); + next->value.name = xstrdup(qdef->name); next->next = NULL; dataitem->next = next; @@ -3568,7 +3568,7 @@ gen_data_item(struct gen_context *ctx, struct expression *expr, qbe_append_def(ctx->out, def); item->value.kind = QV_GLOBAL; item->value.type = &qbe_long; - item->value.name = strdup(def->name); + item->value.name = xstrdup(def->name); } else { free(def); item->value = constl(0); @@ -3607,7 +3607,7 @@ gen_data_item(struct gen_context *ctx, struct expression *expr, qbe_append_def(ctx->out, def); item->value.kind = QV_GLOBAL; item->value.type = &qbe_long; - item->value.name = strdup(def->name); + item->value.name = xstrdup(def->name); } else { free(def); item->value = constl(0); diff --git a/src/genutil.c b/src/genutil.c @@ -81,7 +81,7 @@ mkrtfunc(struct gen_context *ctx, const char *name) { return (struct qbe_value){ .kind = QV_GLOBAL, - .name = strdup(name), + .name = xstrdup(name), .type = ctx->arch.ptr, }; } @@ -91,7 +91,7 @@ mklabel(struct gen_context *ctx, struct qbe_statement *stmt, const char *fmt) { struct qbe_value val; val.kind = QV_LABEL; - val.name = strdup(genl(stmt, &ctx->id, fmt)); + val.name = xstrdup(genl(stmt, &ctx->id, fmt)); return val; } diff --git a/src/identifier.c b/src/identifier.c @@ -48,7 +48,7 @@ identifier_unparse(const struct identifier *ident) free(ns); return str; } - return strdup(ident->name); + return xstrdup(ident->name); } int @@ -106,14 +106,14 @@ ident_to_sym(const struct identifier *ident) free(ns); return str; } - return strdup(ident->name); + return xstrdup(ident->name); } void identifier_dup(struct identifier *new, const struct identifier *ident) { assert(ident && new); - new->name = strdup(ident->name); + new->name = xstrdup(ident->name); if (ident->ns) { new->ns = xcalloc(1, sizeof(struct identifier)); identifier_dup(new->ns, ident->ns); diff --git a/src/lex.c b/src/lex.c @@ -289,7 +289,7 @@ lex_name(struct lexer *lexer, struct token *out) error(&out->loc, "Unknown attribute %s", lexer->buf); } out->token = T_NAME; - out->name = strdup(lexer->buf); + out->name = xstrdup(lexer->buf); } else { out->token = (const char **)token - tokens; } diff --git a/src/mod.c b/src/mod.c @@ -28,7 +28,7 @@ ident_to_env(const struct identifier *ident) free(ns); return str; } - return strdup(ident->name); + return xstrdup(ident->name); } @@ -43,7 +43,7 @@ open_typedefs(struct identifier *ident) if (!version) { version = env; } else { - version = strdup(version); + version = xstrdup(version); free(env); } diff --git a/src/parse.c b/src/parse.c @@ -111,7 +111,7 @@ parse_identifier(struct lexer *lexer, struct identifier *ident, bool trailing) while (!i->name) { switch (lex(lexer, &tok)) { case T_NAME: - i->name = strdup(tok.name); + i->name = xstrdup(tok.name); token_finish(&tok); break; default: @@ -148,7 +148,7 @@ parse_name_list(struct lexer *lexer, struct ast_imports *name) while (more) { struct token tok = {0}; want(lexer, T_NAME, &tok); - name->ident.name = strdup(tok.name); + name->ident.name = xstrdup(tok.name); name->loc = tok.loc; token_finish(&tok); diff --git a/src/qbe.c b/src/qbe.c @@ -150,7 +150,7 @@ qval_dup(const struct qbe_value *val) struct qbe_value *new = xcalloc(1, sizeof(struct qbe_value)); *new = *val; if (val->kind != QV_CONST) { - new->name = strdup(val->name); + new->name = xstrdup(val->name); } return new; } diff --git a/src/type_store.c b/src/type_store.c @@ -169,7 +169,7 @@ struct_insert_field(struct type_store *store, struct struct_field **fields, field = *fields; if (afield->name) { - field->name = strdup(afield->name); + field->name = xstrdup(afield->name); } struct dimensions dim = {0}; if (size_only) { @@ -272,7 +272,7 @@ shift_fields(struct type_store *store, new->type = field->type; new->offset = parent->offset; if (field->name) { - new->name = strdup(field->name); + new->name = xstrdup(field->name); } else { shift_fields(store, NULL, new); } diff --git a/src/util.c b/src/util.c @@ -6,6 +6,7 @@ #undef malloc #undef calloc #undef realloc +#undef strdup const char **sources; @@ -80,6 +81,16 @@ xrealloc(void *p, size_t s) } char * +xstrdup(const char *s) +{ + char *ret = strdup(s); + if (!ret) { + abort(); + } + return ret; +} + +char * getpath(const struct pathspec *paths, size_t npaths) { for (size_t i = 0; i < npaths; i++) { const char *var = "";