Split programmer errors into their own header file for readability and turned the names from string constants into full-fledged uiprivProgrammerError() calls to increase safety.

This commit is contained in:
Pietro Gagliardi 2019-06-02 08:35:40 -04:00
parent 623575f25a
commit ece22bdddc
4 changed files with 51 additions and 23 deletions

View File

@ -36,11 +36,11 @@ uiEvent *uiNewEvent(const uiEventOptions *options)
if (!uiprivCheckInitializedAndThread())
return NULL;
if (options == NULL) {
uiprivProgrammerError(uiprivProgrammerErrorNullPointer, "uiEventOptions", uiprivFunc);
uiprivProgrammerErrorNullPointer("uiEventOptions", uiprivFunc);
return NULL;
}
if (options->Size != sizeof (uiEventOptions)) {
uiprivProgrammerError(uiprivProgrammerErrorWrongStructSize, options->Size, "uiEventOptions");
uiprivProgrammerErrorWrongStructSize(options->Size, "uiEventOptions");
return NULL;
}
e = (uiEvent *) uiprivAlloc(sizeof (uiEvent), "uiEvent");
@ -51,22 +51,22 @@ uiEvent *uiNewEvent(const uiEventOptions *options)
}
#define checkEventNonnull(e, ret) if ((e) == NULL) { \
uiprivProgrammerError(uiprivProgrammerErrorNullPointer, "uiEvent", uiprivFunc); \
uiprivProgrammerErrorNullPointer("uiEvent", uiprivFunc); \
return ret; \
}
#define checkEventNotFiring(e, ret) if ((e)->firing) { \
uiprivProgrammerError(uiprivProgrammerErrorChangingEventDuringFire, uiprivFunc); \
uiprivProgrammerErrorChangingEventDuringFire(uiprivFunc); \
return ret; \
}
static bool checkEventSender(const uiEvent *e, void *sender, const char *func)
{
if (e->opts.Global && sender != NULL) {
uiprivProgrammerError(uiprivProgrammerErrorBadSenderForEvent, "non-NULL", "global", func);
uiprivProgrammerErrorBadSenderForEvent("non-NULL", "global", func);
return false;
}
if (!e->opts.Global && sender == NULL) {
uiprivProgrammerError(uiprivProgrammerErrorBadSenderForEvent, "NULL", "non-global", func);
uiprivProgrammerErrorBadSenderForEvent("NULL", "non-global", func);
return false;
}
return true;
@ -82,7 +82,7 @@ int uiEventAddHandler(uiEvent *e, uiEventHandler handler, void *sender, void *da
checkEventNonnull(e, 0);
checkEventNotFiring(e, 0);
if (handler == NULL) {
uiprivProgrammerError(uiprivProgrammerErrorNullPointer, "uiEventHandler", uiprivFunc);
uiprivProgrammerErrorNullPointer("uiEventHandler", uiprivFunc);
return 0;
}
if (!checkEventSender(e, sender, uiprivFunc))
@ -116,7 +116,7 @@ static struct handler *findHandler(const uiEvent *e, int id, const char *func)
key.id = id;
ret = (struct handler *) uiprivArrayBsearch(&(e->handlers), &key, handlerCmp);
if (ret == NULL)
uiprivProgrammerError(uiprivProgrammerErrorIntIDNotFound, "uiEvent handler", id, func);
uiprivProgrammerErrorIntIDNotFound("uiEvent handler", id, func);
return ret;
}
@ -145,7 +145,7 @@ void uiEventFire(uiEvent *e, void *sender, void *args)
return;
checkEventNonnull(e, /* nothing */);
if (e->firing) {
uiprivProgrammerError(uiprivProgrammerErrorRecursiveEventFire);
uiprivProgrammerErrorRecursiveEventFire();
return;
}
if (!checkEventSender(e, sender, uiprivFunc))

View File

@ -45,7 +45,7 @@ bool uiprivInitReturnErrorf(uiInitError *err, const char *fmt, ...)
void uiQueueMain(void (*f)(void *data), void *data)
{
if (!initialized) {
uiprivProgrammerError(uiprivProgrammerErrorNotInitialized, uiprivFunc);
uiprivProgrammerErrorNotInitialized(uiprivFunc);
return;
}
uiprivSysQueueMain(f, data);
@ -55,11 +55,11 @@ bool uiprivCheckInitializedAndThreadImpl(const char *func)
{
// While it does seem risky to not lock this, if this changes during the execution of this function it means only that it was changed from a different thread, and since it can only change from false to true, an error will be reported anyway.
if (!initialized) {
uiprivProgrammerError(uiprivProgrammerErrorNotInitialized, func);
uiprivProgrammerErrorNotInitialized(func);
return false;
}
if (!uiprivSysCheckThread()) {
uiprivProgrammerError(uiprivProgrammerErrorWrongThread, func);
uiprivProgrammerErrorWrongThread(func);
return false;
}
return true;

38
common/programmererrors.h Normal file
View File

@ -0,0 +1,38 @@
// 2 june 2019
#define uiprivProgrammerErrorNotInitialized(func) \
uiprivProgrammerError("attempt to call %s() before uiInit()", \
func)
#define uiprivProgrammerErrorWrongThread(func) \
uiprivProgrammerError("attempt to call %s() on a thread other than the GUI thread", \
func)
#define uiprivProgrammerErrorWrongStructSize(badSize, structName) \
uiprivProgrammerError("wrong size %" uiprivSizetPrintf " for %s", \
badSize, structName)
#define uiprivProgrammerErrorIndexOutOfRange(badIndex, func) \
uiprivProgrammerError("index %d out of range in %s()", \
badIndex, func)
#define uiprivProgrammerErrorNullPointer(paramDesc, func) \
uiprivProgrammerError("invalid null pointer for %s passed into %s()", \
paramDesc, func)
#define uiprivProgrammerErrorIntIDNotFound(idDesc, badID, func) \
uiprivProgrammerError("%s identifier %d not found in %s()", \
idDesc, badID, func)
// TODO type mismatch
#define uiprivProgrammerErrorBadSenderForEvent(senderDesc, eventDesc, func) \
uiprivProgrammerError("attempt to use a %s sender with a %s event in %s()", \
senderDesc, eventDesc, func)
#define uiprivProgrammerErrorChangingEventDuringFire(func) \
uiprivProgrammerError("attempt to change a uiEvent with %s() while it is firing", \
func)
#define uiprivProgrammerErrorRecursiveEventFire() \
uiprivProgrammerError("attempt to fire a uiEvent while it is already being fired")

View File

@ -60,20 +60,10 @@ extern bool uiprivSysCheckThread(void);
uiprivPrintfFunc(
extern void uiprivInternalError(const char *fmt, ...),
1, 2);
// TODO turn each of these into function-like macros
#define uiprivProgrammerErrorNotInitialized "attempt to call %s() before uiInit()" // arguments: uiprivFunc
#define uiprivProgrammerErrorWrongThread "attempt to call %s() on a thread other than the GUI thread" // arguments: uiprivFunc
#define uiprivProgrammerErrorWrongStructSize "wrong size %" uiprivSizetPrintf " for %s" // arguments: size_t badSize, const char *structName
#define uiprivProgrammerErrorIndexOutOfRange "index %d out of range in %s()" // arguments: int badIndex, uiprivFunc
#define uiprivProgrammerErrorNullPointer "invalid null pointer for %s passed into %s()" // arguments: const char *paramDesc, uiprivFunc
#define uiprivProgrammerErrorIntIDNotFound "%s identifier %d not found in %s()" // arguments: const char *idDesc, int badID, uiprivFunc
// TODO type mismatch
#define uiprivProgrammerErrorBadSenderForEvent "attempt to use a %s sender with a %s event in %s()" // arguments: const char *senderDesc, const char *eventDesc, uiprivFunc
#define uiprivProgrammerErrorChangingEventDuringFire "attempt to change a uiEvent with %s() while it is firing" // arguments: uiprivFunc
#define uiprivProgrammerErrorRecursiveEventFire "attempt to fire a uiEvent while it is already being fired" // no arguments
uiprivPrintfFunc(
extern void uiprivProgrammerError(const char *fmt, ...),
1, 2);
#include "programmererrors.h"
extern void uiprivReportError(const char *prefix, const char *msg, const char *suffix, bool internal);
#ifdef __cplusplus