Skip to content

Commit

Permalink
Silence warnings when compiling as ANSI C.
Browse files Browse the repository at this point in the history
`__func__` is the standard C equivalent to `__FUNCTION__`.

Compiling WildMIDI as ANSI C with GCC warnings enabled causes many
warning messages to be printed which complain about `__FUNCTION__`
not existing in C89. This commit works around it by only using it
when compiling as C99 or newer. In C89, the function string is simply
dummied-out instead.

While I was at it, I used some macro magic to hide most uses of
`__FUNCTION__` and `__LINE__`.
  • Loading branch information
Clownacy committed Jul 2, 2023
1 parent ab9a671 commit 4731033
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 281 deletions.
9 changes: 8 additions & 1 deletion include/wm_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ enum {
extern char * _WM_Global_ErrorS;
extern int _WM_Global_ErrorI;

extern void _WM_GLOBAL_ERROR(const char *func, int lne, int wmerno, const char * wmfor, int error);
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__cplusplus) && __cplusplus >= 201103L)
#define _WM_FUNCTION __func__
#else
#define _WM_FUNCTION "<unknown>"
#endif

#define _WM_GLOBAL_ERROR(wmerno, wmfor, error) _WM_GLOBAL_ERROR_INTERNAL(_WM_FUNCTION, __LINE__, wmerno, wmfor, error)
extern void _WM_GLOBAL_ERROR_INTERNAL(const char *func, int lne, int wmerno, const char * wmfor, int error);

/* sets the global error string to a custom msg */
extern void _WM_ERROR_NEW(const char * wmfmt, ...)
Expand Down
32 changes: 16 additions & 16 deletions src/f_hmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {


if (hmi_size <= 370) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, "file too short", 0);
return NULL;
}

if (memcmp(hmi_data, "HMI-MIDISONG061595", 18)) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_HMI, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_NOT_HMI, NULL, 0);
return NULL;
}

Expand All @@ -94,16 +94,16 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
hmi_track_cnt = hmi_data[228];

if (!hmi_track_cnt) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(no tracks)", 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, "(no tracks)", 0);
return NULL;
}
if (!hmi_bpm) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(bad bpm)", 0);
_WM_GLOBAL_ERROR(WM_ERR_INVALID, "(bad bpm)", 0);
return NULL;
}

if (hmi_size < (370 + (hmi_track_cnt * 17))) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, "file too short", 0);
return NULL;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
for (i = 0; i < hmi_track_cnt; i++) {
/* FIXME: better and/or more size checks??? */
if (data_end - hmi_data < 4) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, "file too short", 0);
goto _hmi_end;
}

Expand All @@ -146,14 +146,14 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
hmi_track_offset[i] += (*hmi_data++ << 24);

if (hmi_size < (hmi_track_offset[i] + 0x5a + 4)) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_HMI, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_NOT_HMI, "file too short", 0);
goto _hmi_end;
}

hmi_addr = hmi_base + hmi_track_offset[i];

if (memcmp(hmi_addr, "HMI-MIDITRACK", 13)) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_HMI, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_NOT_HMI, NULL, 0);
goto _hmi_end;
}

Expand Down Expand Up @@ -196,15 +196,15 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
if (smallest_delta >= 0x7fffffff) {
/* DEBUG */
/* fprintf(stderr,"CRAZY SMALLEST DELTA %u\n", smallest_delta); */
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, NULL, 0);
goto _hmi_end;
}

if ((float)smallest_delta >= 0x7fffffff / samples_per_delta_f) {
/* DEBUG */
/* fprintf(stderr,"INTEGER OVERFLOW (samples_per_delta: %f, smallest_delta: %u)\n", */
/* samples_per_delta_f, smallest_delta); */
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, NULL, 0);
goto _hmi_end;
}

Expand Down Expand Up @@ -251,7 +251,7 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
hmi_data = hmi_base + hmi_track_offset[i];
hmi_delta[i] = 0;
if (hmi_track_offset[i] >= hmi_size) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_HMI, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_NOT_HMI, "file too short", 0);
goto _hmi_end;
}
data_size = hmi_size - hmi_track_offset[i];
Expand All @@ -273,7 +273,7 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
hmi_data += 4;
hmi_track_offset[i] += 4;
if (hmi_tmp > data_size) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_HMI, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_NOT_HMI, "file too short", 0);
goto _hmi_end;
}
data_size -= hmi_tmp;
Expand Down Expand Up @@ -330,7 +330,7 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
} while (*hmi_data > 0x7F);
}
if (!data_size) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_HMI, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_NOT_HMI, "file too short", 0);
goto _hmi_end;
}
note[hmi_tmp].length = (note[hmi_tmp].length << 7) | (*hmi_data & 0x7F);
Expand Down Expand Up @@ -365,7 +365,7 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
} while (*hmi_data > 0x7F);
}
if (!data_size) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_HMI, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_NOT_HMI, "file too short", 0);
goto _hmi_end;
}
hmi_delta[i] = (hmi_delta[i] << 7) | (*hmi_data & 0x7F);
Expand All @@ -387,7 +387,7 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
/* DEBUG */
/* fprintf(stderr,"INTEGER OVERFLOW (samples_per_delta: %f, smallest_delta: %u)\n", */
/* samples_per_delta_f, smallest_delta); */
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, NULL, 0);
goto _hmi_end;
}
subtract_delta = smallest_delta;
Expand All @@ -401,7 +401,7 @@ _WM_ParseNewHmi(const uint8_t *hmi_data, uint32_t hmi_size) {
}

if ((hmi_mdi->reverb = _WM_init_reverb(_WM_SampleRate, _WM_reverb_room_width, _WM_reverb_room_length, _WM_reverb_listen_posx, _WM_reverb_listen_posy)) == NULL) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_MEM, NULL, 0);
goto _hmi_end;
}

Expand Down
24 changes: 12 additions & 12 deletions src/f_hmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,20 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
float sample_remainder = 0;

if (hmp_size < 776) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, "file too short", 0);
return NULL;
}

if (memcmp(hmp_data, "HMIMIDIP", 8)) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_HMP, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_NOT_HMP, NULL, 0);
return NULL;
}
hmp_data += 8;
hmp_size -= 8;

if (!memcmp(hmp_data, "013195", 6)) {
if (hmp_size < 896) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, "file too short", 0);
return NULL;
}
hmp_data += 6;
Expand All @@ -99,7 +99,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
}
for (i = 0; i < zero_cnt; i++) {
if (hmp_data[i] != 0) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_HMP, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_NOT_HMP, NULL, 0);
return NULL;
}
}
Expand All @@ -125,7 +125,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
hmp_size -= 4;

if (!hmp_chunks) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(no tracks)", 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, "(no tracks)", 0);
return NULL;
}

Expand All @@ -149,7 +149,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
hmp_size -= 4;

if (!hmp_bpm) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(bad bpm)", 0);
_WM_GLOBAL_ERROR(WM_ERR_INVALID, "(bad bpm)", 0);
return NULL;
}

Expand Down Expand Up @@ -218,7 +218,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
chunk_ofs[i] += 4;

if (chunk_length[i] > hmp_size) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_HMP, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_NOT_HMP, "file too short", 0);
goto _hmp_end;
}
hmp_size -= chunk_length[i];
Expand Down Expand Up @@ -258,15 +258,15 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
if (smallest_delta >= 0x7fffffff) {
/* DEBUG */
/* fprintf(stderr,"CRAZY SMALLEST DELTA %u\n", smallest_delta); */
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, NULL, 0);
goto _hmp_end;
}

if ((float)smallest_delta >= 0x7fffffff / samples_per_delta_f) {
/* DEBUG */
/* fprintf(stderr,"INTEGER OVERFLOW (samples_per_delta: %f, smallest_delta: %u)\n", */
/* samples_per_delta_f, smallest_delta); */
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, NULL, 0);
goto _hmp_end;
}

Expand Down Expand Up @@ -343,7 +343,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
} while (*hmp_chunk[i] < 0x80);
}
if (! chunk_length[i]) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_HMP, "file too short", 0);
_WM_GLOBAL_ERROR(WM_ERR_NOT_HMP, "file too short", 0);
goto _hmp_end;
}
chunk_delta[i] = chunk_delta[i] + ((*hmp_chunk[i] & 0x7F) << var_len_shift);
Expand All @@ -361,7 +361,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
/* DEBUG */
/* fprintf(stderr,"INTEGER OVERFLOW (samples_per_delta: %f, smallest_delta: %u)\n", */
/* samples_per_delta_f, smallest_delta); */
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_CORUPT, NULL, 0);
goto _hmp_end;
}

Expand All @@ -379,7 +379,7 @@ _WM_ParseNewHmp(const uint8_t *hmp_data, uint32_t hmp_size) {
}

if ((hmp_mdi->reverb = _WM_init_reverb(_WM_SampleRate, _WM_reverb_room_width, _WM_reverb_room_length, _WM_reverb_listen_posx, _WM_reverb_listen_posy)) == NULL) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
_WM_GLOBAL_ERROR(WM_ERR_MEM, NULL, 0);
goto _hmp_end;
}

Expand Down
Loading

0 comments on commit 4731033

Please sign in to comment.