From 760e5a15d32d845bdc3247a6681d2d728b55f3de Mon Sep 17 00:00:00 2001 From: Benjamin Grange Date: Sun, 10 Dec 2023 17:11:31 +0100 Subject: [PATCH] Fix compilation warnings on GNU Linux. --- include/dbg/lang.h | 30 ++------------------------- include/hades.h | 13 ++++++++++-- source/common/game.c | 40 +++++++++++++++--------------------- source/dbg/lang/eval.c | 8 +++++--- source/dbg/lang/lexer.c | 6 ++++-- source/dbg/lang/parser.c | 4 +++- source/dbg/lang/utils.c | 33 +++++++++++++++++++++++++++-- source/gui/windows/menubar.c | 8 ++++---- 8 files changed, 76 insertions(+), 66 deletions(-) diff --git a/include/dbg/lang.h b/include/dbg/lang.h index 39a392f3..02874dc0 100644 --- a/include/dbg/lang.h +++ b/include/dbg/lang.h @@ -44,34 +44,8 @@ enum operator { _OP_BINARY_END_, }; -static int operator_binary_prio[] = { - [OP_BINARY_ASSIGN] = 10, - [OP_BINARY_ADDASSIGN] = 10, - [OP_BINARY_SUBASSIGN] = 10, - [OP_BINARY_MULASSIGN] = 10, - [OP_BINARY_DIVASSIGN] = 10, - - [OP_BINARY_ADD] = 20, - [OP_BINARY_SUB] = 20, - - [OP_BINARY_MUL] = 30, - [OP_BINARY_DIV] = 30, -}; - -static char *operator_name[] = { - [OP_UNARY_PLUS] = "+", - [OP_UNARY_MINUS] = "-", - - [OP_BINARY_ASSIGN] = "=", - [OP_BINARY_ADDASSIGN] = "+=", - [OP_BINARY_SUBASSIGN] = "-=", - [OP_BINARY_MULASSIGN] = "*=", - [OP_BINARY_DIVASSIGN] = "/=", - [OP_BINARY_ADD] = "+", - [OP_BINARY_SUB] = "-", - [OP_BINARY_MUL] = "*", - [OP_BINARY_DIV] = "/", -}; +extern int operator_binary_prio[]; +extern char *operator_name[]; struct token { enum { diff --git a/include/hades.h b/include/hades.h index c215af2b..4650f4a1 100644 --- a/include/hades.h +++ b/include/hades.h @@ -9,6 +9,8 @@ #pragma once +#define _GNU_SOURCE + #include #include #include @@ -93,8 +95,15 @@ static char const * const modules_str[] = { __LINE__ \ ); \ } \ - } \ - while (0) + } while (0) + +#define hs_format(fmt, ...) \ + ({ \ + char *__tmp; \ + \ + hs_assert(-1 != asprintf(&__tmp, fmt, ##__VA_ARGS__));\ + __tmp; \ + }) /* ✨ Variadic macro magic ✨ */ #define XSTR(...) #__VA_ARGS__ diff --git a/source/common/game.c b/source/common/game.c index 6901a49a..8c451a2f 100644 --- a/source/common/game.c +++ b/source/common/game.c @@ -214,22 +214,20 @@ app_game_configure_bios( char *error_msg; if (!app->file.bios_path) { - hs_assert(-1 != asprintf( - &error_msg, + error_msg = hs_format( "no BIOS found.\n\nPlease download and select a valid Nintendo GBA Bios using \"File\" -> \"Open BIOS\"." - )); + ); gui_new_error(app, error_msg); return (true); } file = hs_fopen(app->file.bios_path, "rb"); if (!file) { - hs_assert(-1 != asprintf( - &error_msg, + error_msg = hs_format( "failed to open %s: %s.", app->file.bios_path, strerror(errno) - )); + ); gui_new_error(app, error_msg); return (true); } @@ -247,12 +245,11 @@ app_game_configure_bios( hs_assert(data); if (fread(data, 1, BIOS_SIZE, file) != BIOS_SIZE) { - hs_assert(-1 != asprintf( - &error_msg, + error_msg = hs_format( "failed to read %s: %s.", app->file.bios_path, strerror(errno) - )); + ); gui_new_error(app, error_msg); free(data); return (true); @@ -277,12 +274,11 @@ app_game_configure_rom( file = hs_fopen(rom_path, "rb"); if (!file) { - hs_assert(-1 != asprintf( - &error_msg, + error_msg = hs_format( "failed to open %s: %s.", rom_path, strerror(errno) - )); + ); gui_new_error(app, error_msg); return (true); } @@ -301,12 +297,11 @@ app_game_configure_rom( hs_assert(data); if (fread(data, 1, file_len, file) != file_len) { - hs_assert(-1 != asprintf( - &error_msg, + error_msg = hs_format( "failed to read %s: %s.", rom_path, strerror(errno) - )); + ); gui_new_error(app, error_msg); free(data); return (true); @@ -356,12 +351,11 @@ app_game_configure_backup( app->emulation.backup_file = hs_fopen(backup_path, "wb+"); if (!app->emulation.backup_file) { - hs_assert(-1 != asprintf( - &error_msg, + error_msg = hs_format( "failed to create %s: %s.", backup_path, strerror(errno) - )); + ); gui_new_error(app, error_msg); return (true); } @@ -440,23 +434,21 @@ app_game_configure( app->file.qsaves[i].mtime = NULL; - hs_assert(-1 != asprintf( - &app->file.qsaves[i].path, + app->file.qsaves[i].path = hs_format( "%.*s.%zu.hds", (int)basename_len, rom_path, i + 1 - )); + ); } app->file.flush_qsaves_cache = true; - hs_assert(-1 != asprintf( - &backup_path, + backup_path = hs_format( "%.*s.sav", (int)basename_len, rom_path - )); + ); if (app_game_configure_bios(app) || app_game_configure_rom(app, rom_path) diff --git a/source/dbg/lang/eval.c b/source/dbg/lang/eval.c index 4a6a406c..52f022e5 100644 --- a/source/dbg/lang/eval.c +++ b/source/dbg/lang/eval.c @@ -7,6 +7,8 @@ ** \******************************************************************************/ +#define _GNU_SOURCE + #include #include "hades.h" #include "app.h" @@ -29,7 +31,7 @@ debugger_lang_eval_node( variable = debugger_lang_variables_lookup(app, node->value.identifier); if (!variable) { free(eval->error); - asprintf(&eval->error, "Undefined variable \"%s\"", node->value.identifier); + eval->error = hs_format("Undefined variable \"%s\"", node->value.identifier); return (0); } @@ -61,13 +63,13 @@ debugger_lang_eval_node( variable = debugger_lang_variables_lookup(app, node->lhs->value.identifier); if (!variable) { free(eval->error); - asprintf(&eval->error, "Undefined variable \"%s\"", node->value.identifier); + eval->error = hs_format("Undefined variable \"%s\"", node->value.identifier); return (0); } if (!variable->mutable) { free(eval->error); - asprintf(&eval->error, "Variable \"%s\" is not mutable.", node->value.identifier); + eval->error = hs_format("Variable \"%s\" is not mutable.", node->value.identifier); return (0); } diff --git a/source/dbg/lang/lexer.c b/source/dbg/lang/lexer.c index 7022e084..6fb9c64a 100644 --- a/source/dbg/lang/lexer.c +++ b/source/dbg/lang/lexer.c @@ -7,6 +7,8 @@ ** \******************************************************************************/ +#define _GNU_SOURCE + #include #include #include "hades.h" @@ -93,7 +95,7 @@ debugger_lang_lexe( } i += end - (input + i); if (isalpha(input[i])) { - asprintf(&lexer->error, "Invalid character \'%c\'", input[i]); + lexer->error = hs_format("Invalid character \'%c\'", input[i]); return ; } break; @@ -161,7 +163,7 @@ debugger_lang_lexe( break; }; default: { - asprintf(&lexer->error, "Invalid character \'%c\'", input[i]); + lexer->error = hs_format("Invalid character \'%c\'", input[i]); return ; }; } diff --git a/source/dbg/lang/parser.c b/source/dbg/lang/parser.c index a20b42a1..b5dc746b 100644 --- a/source/dbg/lang/parser.c +++ b/source/dbg/lang/parser.c @@ -7,6 +7,8 @@ ** \******************************************************************************/ +#define _GNU_SOURCE + #include #include "hades.h" #include "dbg/lang.h" @@ -121,7 +123,7 @@ debugger_lang_try_parse_binary_op( ast->token = token->next; // Eat operator if (!ast->token) { free(ast->error); - asprintf(&ast->error, "Missing right-handside value for operator \"%s\"", operator_name[op->value.operator]); + ast->error = hs_format("Missing right-handside value for operator \"%s\"", operator_name[op->value.operator]); return (op); } diff --git a/source/dbg/lang/utils.c b/source/dbg/lang/utils.c index 19b58b08..02f533e1 100644 --- a/source/dbg/lang/utils.c +++ b/source/dbg/lang/utils.c @@ -11,6 +11,35 @@ #include "hades.h" #include "dbg/lang.h" +int operator_binary_prio[] = { + [OP_BINARY_ASSIGN] = 10, + [OP_BINARY_ADDASSIGN] = 10, + [OP_BINARY_SUBASSIGN] = 10, + [OP_BINARY_MULASSIGN] = 10, + [OP_BINARY_DIVASSIGN] = 10, + + [OP_BINARY_ADD] = 20, + [OP_BINARY_SUB] = 20, + + [OP_BINARY_MUL] = 30, + [OP_BINARY_DIV] = 30, +}; + +char *operator_name[] = { + [OP_UNARY_PLUS] = "+", + [OP_UNARY_MINUS] = "-", + + [OP_BINARY_ASSIGN] = "=", + [OP_BINARY_ADDASSIGN] = "+=", + [OP_BINARY_SUBASSIGN] = "-=", + [OP_BINARY_MULASSIGN] = "*=", + [OP_BINARY_DIVASSIGN] = "/=", + [OP_BINARY_ADD] = "+", + [OP_BINARY_SUB] = "-", + [OP_BINARY_MUL] = "*", + [OP_BINARY_DIV] = "/", +}; + void debugger_lang_dump_lexer( struct lexer const *lexer @@ -22,7 +51,7 @@ debugger_lang_dump_lexer( printf("Token { "); switch (token->kind) { case TOKEN_LITTERAL: { - printf("Litteral (%lli)", token->value.litteral); + printf("Litteral (%lli)", (long long int)token->value.litteral); break; }; case TOKEN_IDENTIFIER: { @@ -66,7 +95,7 @@ debugger_lang_dump_ast_raw( ) { switch (node->kind) { case NODE_LITTERAL: { - printf("Node { Litteral (%lli) }\n", node->value.litteral); + printf("Node { Litteral (%lli) }\n", (long long int)node->value.litteral); break; }; case NODE_VARIABLE: { diff --git a/source/gui/windows/menubar.c b/source/gui/windows/menubar.c index 2292c50b..dbd22353 100644 --- a/source/gui/windows/menubar.c +++ b/source/gui/windows/menubar.c @@ -145,9 +145,9 @@ gui_win_menubar_emulation( char *text; if (app->file.qsaves[i].exist && app->file.qsaves[i].mtime) { - hs_assert(asprintf(&text, "%zu: %s", i + 1, app->file.qsaves[i].mtime) != -1); + text = hs_format("%zu: %s", i + 1, app->file.qsaves[i].mtime); } else { - hs_assert(asprintf(&text, "%zu: ", i + 1) != -1); + text = hs_format("%zu: ", i + 1); } hs_assert(text); @@ -169,9 +169,9 @@ gui_win_menubar_emulation( char *text; if (app->file.qsaves[i].exist && app->file.qsaves[i].mtime) { - hs_assert(asprintf(&text, "%zu: %s", i + 1, app->file.qsaves[i].mtime) != -1); + text = hs_format("%zu: %s", i + 1, app->file.qsaves[i].mtime); } else { - hs_assert(asprintf(&text, "%zu: ", i + 1) != -1); + text = hs_format("%zu: ", i + 1); } hs_assert(text);