Call user main from wrapper. May not be a great idea.

This commit is contained in:
2026-01-18 00:16:09 -05:00
parent c840536e1d
commit 350d198015
6 changed files with 37 additions and 9 deletions

View File

@@ -36,9 +36,11 @@ target_compile_definitions(akerror
add_executable(test_err_catch tests/err_catch.c) add_executable(test_err_catch tests/err_catch.c)
add_executable(test_err_cleanup tests/err_cleanup.c) add_executable(test_err_cleanup tests/err_cleanup.c)
add_executable(test_err_trace tests/err_trace.c) add_executable(test_err_trace tests/err_trace.c)
add_executable(test_err_unhandled tests/err_unhandled.c)
add_test(NAME err_catch COMMAND test_err_catch) add_test(NAME err_catch COMMAND test_err_catch)
add_test(NAME err_cleanup COMMAND test_err_cleanup) add_test(NAME err_cleanup COMMAND test_err_cleanup)
add_test(NAME err_trace COMMAND test_err_trace) add_test(NAME err_trace COMMAND test_err_trace)
add_test(NAME err_unhandled COMMAND test_err_unhandled)
# Specify include directories for the library's headers (if applicable) # Specify include directories for the library's headers (if applicable)
target_include_directories(akerror PUBLIC target_include_directories(akerror PUBLIC
@@ -48,6 +50,7 @@ target_include_directories(akerror PUBLIC
target_link_libraries(test_err_catch PRIVATE akerror) target_link_libraries(test_err_catch PRIVATE akerror)
target_link_libraries(test_err_cleanup PRIVATE akerror) target_link_libraries(test_err_cleanup PRIVATE akerror)
target_link_libraries(test_err_trace PRIVATE akerror) target_link_libraries(test_err_trace PRIVATE akerror)
target_link_libraries(test_err_unhandled PRIVATE akerror)
set(main_lib_dest "lib/my_library-${MY_LIBRARY_VERSION}") set(main_lib_dest "lib/my_library-${MY_LIBRARY_VERSION}")
install(TARGETS akerror EXPORT akerror DESTINATION "lib/") install(TARGETS akerror EXPORT akerror DESTINATION "lib/")

View File

@@ -55,6 +55,7 @@ typedef struct
bool reported; bool reported;
char stacktracebuf[AKERR_MAX_ERROR_STACKTRACE_BUF_LENGTH]; char stacktracebuf[AKERR_MAX_ERROR_STACKTRACE_BUF_LENGTH];
char *stacktracebufptr; char *stacktracebufptr;
int stacktracebufremaining;
} akerr_ErrorContext; } akerr_ErrorContext;
#define AKERR_NOIGNORE __attribute__((warn_unused_result)) #define AKERR_NOIGNORE __attribute__((warn_unused_result))
@@ -67,6 +68,7 @@ extern akerr_ErrorUnhandledErrorHandler akerr_handler_unhandled_error;
extern akerr_ErrorLogFunction akerr_log_method; extern akerr_ErrorLogFunction akerr_log_method;
extern akerr_ErrorContext *__akerr_last_ignored; extern akerr_ErrorContext *__akerr_last_ignored;
akerr_ErrorContext AKERR_NOIGNORE *akerr_user_main(int argc, char **argv);
akerr_ErrorContext AKERR_NOIGNORE *akerr_release_error(akerr_ErrorContext *ptr); akerr_ErrorContext AKERR_NOIGNORE *akerr_release_error(akerr_ErrorContext *ptr);
akerr_ErrorContext AKERR_NOIGNORE *akerr_next_error(); akerr_ErrorContext AKERR_NOIGNORE *akerr_next_error();
char *akerr_name_for_status(int status, char *name); char *akerr_name_for_status(int status, char *name);

View File

@@ -14,6 +14,28 @@ char __AKERR_ERROR_NAMES[AKERR_MAX_ERR_VALUE+1][AKERR_MAX_ERROR_NAME_LENGTH];
akerr_ErrorContext AKERR_ARRAY_ERROR[AKERR_MAX_ARRAY_ERROR]; akerr_ErrorContext AKERR_ARRAY_ERROR[AKERR_MAX_ARRAY_ERROR];
int main(int argc, char **argv)
{
int i = 0;
PREPARE_ERROR(e);
ATTEMPT {
CATCH(e, akerr_user_main(argc, argv));
if ( e == NULL || e->status == 0 ) {
/* User code claims everything went fine, doublecheck the error array before we quit */
for ( i = AKERR_MAX_ARRAY_ERROR - 1; i >= 0; i-- ) {
if ( AKERR_ARRAY_ERROR[i].status != 0 && AKERR_ARRAY_ERROR[i].refcount != 0 ) {
akerr_log_method("%s:%s:%d: Found unhandled and unreported error in the array at index %d\n", __FILE__, (char *)__func__, __LINE__, i);
e = &AKERR_ARRAY_ERROR[i];
}
}
}
} CLEANUP {
} PROCESS(e) {
} FINISH_NORETURN(e);
SUCCEED(e);
return 0;
}
void akerr_default_logger(const char *fmt, ...) void akerr_default_logger(const char *fmt, ...)
{ {
#if defined(AKERR_USE_STDLIB) && AKERR_USE_STDLIB == 1 #if defined(AKERR_USE_STDLIB) && AKERR_USE_STDLIB == 1

View File

@@ -23,7 +23,7 @@ akerr_ErrorContext *func1(void)
} }
int main(void) akerr_ErrorContext *akerr_user_main(int argc, char **argv)
{ {
PREPARE_ERROR(errctx); PREPARE_ERROR(errctx);
ATTEMPT { ATTEMPT {
@@ -32,5 +32,6 @@ int main(void)
} PROCESS(errctx) { } PROCESS(errctx) {
} HANDLE(errctx, AKERR_NULLPOINTER) { } HANDLE(errctx, AKERR_NULLPOINTER) {
akerr_log_method("Caught exception"); akerr_log_method("Caught exception");
} FINISH_NORETURN(errctx); } FINISH(errctx, true);
SUCCEED(errctx);
} }

View File

@@ -25,7 +25,7 @@ akerr_ErrorContext *func1(void)
SUCCEED_RETURN(errctx); SUCCEED_RETURN(errctx);
} }
int main(void) akerr_ErrorContext *akerr_user_main(int argc, char **argv)
{ {
x = 12345; x = 12345;
PREPARE_ERROR(errctx); PREPARE_ERROR(errctx);
@@ -35,9 +35,8 @@ int main(void)
} PROCESS(errctx) { } PROCESS(errctx) {
} HANDLE(errctx, AKERR_NULLPOINTER) { } HANDLE(errctx, AKERR_NULLPOINTER) {
if ( x == 0 ) { if ( x == 0 ) {
fprintf(stderr, "Cleanup works\n"); FAIL_RETURN(errctx, AKERR_API, "Cleanup does not work");
return 0;
} }
return 1; } FINISH(errctx, true);
} FINISH_NORETURN(errctx); SUCCEED(errctx);
} }

View File

@@ -23,12 +23,13 @@ akerr_ErrorContext *func1(void)
} }
int main(void) akerr_ErrorContext *akerr_user_main(int argc, char **argv)
{ {
PREPARE_ERROR(errctx); PREPARE_ERROR(errctx);
ATTEMPT { ATTEMPT {
CATCH(errctx, func1()); CATCH(errctx, func1());
} CLEANUP { } CLEANUP {
} PROCESS(errctx) { } PROCESS(errctx) {
} FINISH_NORETURN(errctx); } FINISH(errctx, true);
SUCCEED(errctx);
} }