diff --git a/libtcc.c b/libtcc.c index b3c4fc8..db30223 100644 --- a/libtcc.c +++ b/libtcc.c @@ -518,13 +518,22 @@ static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap) s1->nb_errors++; } -LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, - void (*error_func)(void *opaque, const char *msg)) +LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc error_func) { s->error_opaque = error_opaque; s->error_func = error_func; } +LIBTCCAPI TCCErrorFunc tcc_get_error_func(TCCState *s) +{ + return s->error_func; +} + +LIBTCCAPI void *tcc_get_error_opaque(TCCState *s) +{ + return s->error_opaque; +} + /* error without aborting current compilation */ PUB_FUNC void tcc_error_noabort(const char *fmt, ...) { diff --git a/libtcc.h b/libtcc.h index 0c6bb3d..e164f5c 100644 --- a/libtcc.h +++ b/libtcc.h @@ -13,6 +13,8 @@ struct TCCState; typedef struct TCCState TCCState; +typedef void (*TCCErrorFunc)(void *opaque, const char *msg); + /* create a new TCC compilation context */ LIBTCCAPI TCCState *tcc_new(void); @@ -23,8 +25,13 @@ LIBTCCAPI void tcc_delete(TCCState *s); LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path); /* set error/warning display callback */ -LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, - void (*error_func)(void *opaque, const char *msg)); +LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc error_func); + +/* return error/warning callback */ +LIBTCCAPI TCCErrorFunc tcc_get_error_func(TCCState *s); + +/* return error/warning callback opaque pointer */ +LIBTCCAPI void *tcc_get_error_opaque(TCCState *s); /* set options as from command line (multiple supported) */ LIBTCCAPI void tcc_set_options(TCCState *s, const char *str); diff --git a/tests/libtcc_test.c b/tests/libtcc_test.c index 480d314..d21c7fe 100644 --- a/tests/libtcc_test.c +++ b/tests/libtcc_test.c @@ -6,9 +6,15 @@ #include #include #include +#include #include "libtcc.h" +void handle_error(void *opaque, const char *msg) +{ + fprintf(opaque, "%s\n", msg); +} + /* this function is called by the generated code */ int add(int a, int b) { @@ -53,6 +59,14 @@ int main(int argc, char **argv) exit(1); } + assert(tcc_get_error_func(s) == NULL); + assert(tcc_get_error_opaque(s) == NULL); + + tcc_set_error_func(s, stderr, handle_error); + + assert(tcc_get_error_func(s) == handle_error); + assert(tcc_get_error_opaque(s) == stderr); + /* if tcclib.h and libtcc1.a are not installed, where can we find them */ for (i = 1; i < argc; ++i) { char *a = argv[i];