Add error_func and error_opaque getters to libtcc

mob
Luc Everse 2019-10-14 09:36:14 +02:00
parent 944c4003bd
commit 491773ac58
No known key found for this signature in database
GPG Key ID: D705AEB78437F3F6
3 changed files with 34 additions and 4 deletions

View File

@ -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, ...)
{

View File

@ -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);

View File

@ -6,9 +6,15 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#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];