Skip to content

Commit

Permalink
Fix compilation warnings on GNU Linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Grange committed Dec 10, 2023
1 parent 3a739bc commit 760e5a1
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 66 deletions.
30 changes: 2 additions & 28 deletions include/dbg/lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions include/hades.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#pragma once

#define _GNU_SOURCE

#include <stdatomic.h>
#include <stdio.h>
#include <stdint.h>
Expand Down Expand Up @@ -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__
Expand Down
40 changes: 16 additions & 24 deletions source/common/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions source/dbg/lang/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
**
\******************************************************************************/

#define _GNU_SOURCE

#include <string.h>
#include "hades.h"
#include "app.h"
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions source/dbg/lang/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
**
\******************************************************************************/

#define _GNU_SOURCE

#include <ctype.h>
#include <string.h>
#include "hades.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ;
};
}
Expand Down
4 changes: 3 additions & 1 deletion source/dbg/lang/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
**
\******************************************************************************/

#define _GNU_SOURCE

#include <string.h>
#include "hades.h"
#include "dbg/lang.h"
Expand Down Expand Up @@ -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);
}

Expand Down
33 changes: 31 additions & 2 deletions source/dbg/lang/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down
8 changes: 4 additions & 4 deletions source/gui/windows/menubar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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: <empty>", i + 1) != -1);
text = hs_format("%zu: <empty>", i + 1);
}

hs_assert(text);
Expand All @@ -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: <empty>", i + 1) != -1);
text = hs_format("%zu: <empty>", i + 1);
}

hs_assert(text);
Expand Down

0 comments on commit 760e5a1

Please sign in to comment.