From 60ff06fb6d5d063e0a4bfaffbefa5ed22e2921fd Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Wed, 21 Aug 2019 22:06:13 +0300 Subject: [PATCH] Reorganize error functions and includes These functions are complicated because they need to be marked "noreturn" for the compiler. There are several fundamentally incompatible ways of doing that. Since we have only a few "noreturn" functions, let's just list all of those in one header file of which there is a completely different version for each compiler. --- c/algo_color.c | 2 ++ c/builtins.c | 2 ++ c/cvalues.h | 7 ------- c/dtypes.h | 16 ---------------- c/env_unix.c | 2 ++ c/error.h | 15 +++++++++++++++ c/error_dmc.h | 17 +++++++++++++++++ c/error_gnuc.h | 13 +++++++++++++ c/error_msc.h | 13 +++++++++++++ c/error_watcomc.h | 17 +++++++++++++++++ c/flisp.c | 4 ++++ c/flisp.h | 5 ----- c/iostream.c | 2 ++ c/libraries.c | 1 + c/os_unix.c | 2 ++ c/os_unix_process.c | 2 ++ c/os_windows.c | 1 + c/string.c | 2 ++ c/table.c | 2 ++ c/text_ini.c | 2 ++ 20 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 c/error.h create mode 100644 c/error_dmc.h create mode 100644 c/error_gnuc.h create mode 100644 c/error_msc.h create mode 100644 c/error_watcomc.h diff --git a/c/algo_color.c b/c/algo_color.c index 020b811..d85605e 100644 --- a/c/algo_color.c +++ b/c/algo_color.c @@ -28,6 +28,8 @@ #include "flisp.h" +#include "error.h" + #include "argcount.h" #include "builtins.h" diff --git a/c/builtins.c b/c/builtins.c index d313451..127a834 100644 --- a/c/builtins.c +++ b/c/builtins.c @@ -29,6 +29,8 @@ #include "flisp.h" +#include "error.h" + #include "argcount.h" #include "libraries.h" diff --git a/c/cvalues.h b/c/cvalues.h index 39eb559..df20872 100644 --- a/c/cvalues.h +++ b/c/cvalues.h @@ -1363,13 +1363,6 @@ int numeric_compare(value_t a, value_t b, int eq, int eqnans, char *fname) return 1; } -STATIC_NORETURN(void, DivideByZeroError()); - -static void DivideByZeroError(void) -{ - lerror(DivideError, "/: division by zero"); -} - static value_t fl_div2(value_t a, value_t b) { double da, db; diff --git a/c/dtypes.h b/c/dtypes.h index e991af1..bf7229f 100644 --- a/c/dtypes.h +++ b/c/dtypes.h @@ -29,22 +29,6 @@ typedef double double_t; #define BITS64 #endif -#ifdef __GNUC__ -#define EXTERN_NORETURN(ret, args) extern ret args __attribute__((__noreturn__)) -#define STATIC_NORETURN(ret, args) static ret args __attribute__((__noreturn__)) -#endif - -#ifdef _MSC_VER -#define EXTERN_NORETURN(ret, args) __declspec(noreturn) extern ret args -#define STATIC_NORETURN(ret, args) __declspec(noreturn) static ret args -#endif - -#ifdef __WATCOMC__ -#pragma aux noreturn aborts; -#define EXTERN_NORETURN(ret, args) extern ret __pragma("noreturn") args -#define STATIC_NORETURN(ret, args) static ret __pragma("noreturn") args -#endif - #define LLT_ALLOC(n) malloc(n) #define LLT_REALLOC(p, n) realloc((p), (n)) #define LLT_FREE(x) free(x) diff --git a/c/env_unix.c b/c/env_unix.c index 6dea63c..fb33ff0 100644 --- a/c/env_unix.c +++ b/c/env_unix.c @@ -32,6 +32,8 @@ #include "flisp.h" +#include "error.h" + #include "argcount.h" static const struct utsname *get_global_uname(void) diff --git a/c/error.h b/c/error.h new file mode 100644 index 0000000..cddf45a --- /dev/null +++ b/c/error.h @@ -0,0 +1,15 @@ +#ifdef __DMC__ +#include "error_dmc.h" +#endif + +#ifdef __GNUC__ +#include "error_gnuc.h" +#endif + +#ifdef _MSC_VER +#include "error_msc.h" +#endif + +#ifdef __WATCOMC__ +#include "error_watcomc.h" +#endif diff --git a/c/error_dmc.h b/c/error_dmc.h new file mode 100644 index 0000000..9510883 --- /dev/null +++ b/c/error_dmc.h @@ -0,0 +1,17 @@ +extern void DivideByZeroError(void); +#pragma noreturn(DivideByZeroError) + +extern void lerrorf(value_t e, char *format, ...); +#pragma noreturn(lerrorf) + +extern void lerror(value_t e, const char *msg); +#pragma noreturn(lerror) + +extern void fl_raise(value_t e); +#pragma noreturn(fl_raise) + +extern void type_error(char *fname, char *expected, value_t got); +#pragma noreturn(type_error) + +extern void bounds_error(char *fname, value_t arr, value_t ind); +#pragma noreturn(bounds_error) diff --git a/c/error_gnuc.h b/c/error_gnuc.h new file mode 100644 index 0000000..5cf3287 --- /dev/null +++ b/c/error_gnuc.h @@ -0,0 +1,13 @@ +void DivideByZeroError(void) __attribute__((__noreturn__)); + +void lerrorf(value_t e, char *format, ...) __attribute__((__noreturn__)); + +void lerror(value_t e, const char *msg) __attribute__((__noreturn__)); + +void fl_raise(value_t e) __attribute__((__noreturn__)); + +void type_error(char *fname, char *expected, value_t got) +__attribute__((__noreturn__)); + +void bounds_error(char *fname, value_t arr, value_t ind) +__attribute__((__noreturn__)); diff --git a/c/error_msc.h b/c/error_msc.h new file mode 100644 index 0000000..289f9c3 --- /dev/null +++ b/c/error_msc.h @@ -0,0 +1,13 @@ +__declspec(noreturn) extern void DivideByZeroError(void); + +__declspec(noreturn) extern void lerrorf(value_t e, char *format, ...); + +__declspec(noreturn) extern void lerror(value_t e, const char *msg); + +__declspec(noreturn) extern void fl_raise(value_t e); + +__declspec(noreturn) extern void type_error(char *fname, char *expected, + value_t got); + +__declspec(noreturn) extern void bounds_error(char *fname, value_t arr, + value_t ind); diff --git a/c/error_watcomc.h b/c/error_watcomc.h new file mode 100644 index 0000000..cf04235 --- /dev/null +++ b/c/error_watcomc.h @@ -0,0 +1,17 @@ +#pragma aux DivideByZeroError aborts; +extern void DivideByZeroError(void); + +#pragma aux lerrorf aborts; +extern void lerrorf(value_t e, char *format, ...); + +#pragma aux lerror aborts; +extern void lerror(value_t e, const char *msg); + +#pragma aux fl_raise aborts; +extern void fl_raise(value_t e); + +#pragma aux type_error aborts; +extern void type_error(char *fname, char *expected, value_t got); + +#pragma aux bounds_error aborts; +extern void bounds_error(char *fname, value_t arr, value_t ind); diff --git a/c/flisp.c b/c/flisp.c index 865c3c3..b3bd3da 100644 --- a/c/flisp.c +++ b/c/flisp.c @@ -63,6 +63,8 @@ #include "flisp.h" +#include "error.h" + #include "argcount.h" #include "env.h" #include "opcodes.h" @@ -247,6 +249,8 @@ void bounds_error(char *fname, value_t arr, value_t ind) fl_raise(fl_listn(4, BoundsError, symbol(fname), arr, ind)); } +void DivideByZeroError(void) { lerror(DivideError, "/: division by zero"); } + // safe cast operators // -------------------------------------------------------- diff --git a/c/flisp.h b/c/flisp.h index dc87e8c..722cf59 100644 --- a/c/flisp.h +++ b/c/flisp.h @@ -190,13 +190,8 @@ extern value_t fl_lasterror; #define FL_CATCH_EXTERN \ else for (l__ca = 1; l__ca; l__ca = 0, fl_restorestate(&_ctx)) -EXTERN_NORETURN(void, lerrorf(value_t e, char *format, ...)); -EXTERN_NORETURN(void, lerror(value_t e, const char *msg)); void fl_savestate(struct fl_exception_context *_ctx); void fl_restorestate(struct fl_exception_context *_ctx); -EXTERN_NORETURN(void, fl_raise(value_t e)); -EXTERN_NORETURN(void, type_error(char *fname, char *expected, value_t got)); -EXTERN_NORETURN(void, bounds_error(char *fname, value_t arr, value_t ind)); extern value_t ArgError, IOError, KeyError, MemoryError, EnumerationError; extern value_t UnboundError; diff --git a/c/iostream.c b/c/iostream.c index 9f8a3a7..72c9c69 100644 --- a/c/iostream.c +++ b/c/iostream.c @@ -25,6 +25,8 @@ #include "flisp.h" +#include "error.h" + #include "argcount.h" static value_t iostreamsym, rdsym, wrsym, apsym, crsym, truncsym; diff --git a/c/libraries.c b/c/libraries.c index 9305717..7b5ec8c 100644 --- a/c/libraries.c +++ b/c/libraries.c @@ -37,6 +37,7 @@ #include "buf.h" #include "env.h" +#include "error.h" #include "opcodes.h" #include "builtins.h" diff --git a/c/os_unix.c b/c/os_unix.c index 84531f3..f810ae9 100644 --- a/c/os_unix.c +++ b/c/os_unix.c @@ -31,6 +31,8 @@ #include "flisp.h" +#include "error.h" + #include "argcount.h" #include "os.h" diff --git a/c/os_unix_process.c b/c/os_unix_process.c index c4d0913..7c2435a 100644 --- a/c/os_unix_process.c +++ b/c/os_unix_process.c @@ -32,6 +32,8 @@ #include "flisp.h" +#include "error.h" + #include "argcount.h" #include "os.h" diff --git a/c/os_windows.c b/c/os_windows.c index 4ea6c83..49b4ffc 100644 --- a/c/os_windows.c +++ b/c/os_windows.c @@ -31,6 +31,7 @@ #include "flisp.h" +#include "error.h" #include "os.h" void path_to_dirname(char *path) diff --git a/c/string.c b/c/string.c index e550635..9c8bbab 100644 --- a/c/string.c +++ b/c/string.c @@ -32,6 +32,8 @@ #include "flisp.h" +#include "error.h" + #include "argcount.h" value_t fl_stringp(value_t *args, uint32_t nargs) diff --git a/c/table.c b/c/table.c index cc6f9cc..f9b6266 100644 --- a/c/table.c +++ b/c/table.c @@ -25,6 +25,8 @@ #include "flisp.h" +#include "error.h" + #include "argcount.h" #include "equalhash.h" diff --git a/c/text_ini.c b/c/text_ini.c index 7a4ad8c..50d877f 100644 --- a/c/text_ini.c +++ b/c/text_ini.c @@ -28,6 +28,8 @@ #include "flisp.h" +#include "error.h" + #include "argcount.h" #include "buf.h"