diff --git a/src/SConscript b/src/SConscript index e192b05e182b0020ac7f931f68244b300b93b9bc..05ffa983674488db41cc0c6f32f642c2f43e30f8 100644 --- a/src/SConscript +++ b/src/SConscript @@ -5,6 +5,7 @@ Import('env testruns') dist_headers = [ "hammer.h", "allocator.h", + "compiler_specifics.h", "glue.h", "internal.h" ] @@ -61,6 +62,7 @@ misc_hammer_parts = [ 'desugar.c', 'glue.c', 'hammer.c', + 'platform_bsdlike.c', 'pprint.c', 'registry.c', 'system_allocator.c'] diff --git a/src/backends/packrat.c b/src/backends/packrat.c index 33082c6c278beb09b2abf767e5314d18ab471db4..e6f86f2957e5988dd94557534ab66b6050fda915 100644 --- a/src/backends/packrat.c +++ b/src/backends/packrat.c @@ -126,7 +126,7 @@ HParseResult* grow(HParserCacheKey *k, HParseState *state, HRecursionHead *head) h_hashtable_put(state->recursion_heads, &k->input_pos, head); HParserCacheValue *old_cached = h_hashtable_get(state->cache, k); if (!old_cached || PC_LEFT == old_cached->value_type) - errx(1, "impossible match"); + h_platform_errx(1, "impossible match"); HParseResult *old_res = old_cached->right; // rewind the input @@ -148,7 +148,7 @@ HParseResult* grow(HParserCacheKey *k, HParseState *state, HRecursionHead *head) state->input_stream = cached->input_stream; return cached->right; } else { - errx(1, "impossible match"); + h_platform_errx(1, "impossible match"); } } } else { @@ -173,7 +173,7 @@ HParseResult* lr_answer(HParserCacheKey *k, HParseState *state, HLeftRec *growab return grow(k, state, growable->head); } } else { - errx(1, "lrAnswer with no head"); + h_platform_errx(1, "lrAnswer with no head"); } } diff --git a/src/compiler_specifics.h b/src/compiler_specifics.h new file mode 100644 index 0000000000000000000000000000000000000000..ed09d664fa52557ce5505f789c37ffb881a5f753 --- /dev/null +++ b/src/compiler_specifics.h @@ -0,0 +1,16 @@ +#ifndef HAMMER_COMPILER_SPECIFICS__H +#define HAMMER_COMPILER_SPECIFICS__H + +#if defined(__clang__) || defined(__GNUC__) +#define H_GCC_ATTRIBUTE(x) __attribute__(x) +#else +#define H_GCC_ATTRIBUTE(x) +#endif + +#if defined(_MSC_VER) +#define H_MSVC_DECLSPEC(x) __declspec(x) +#else +#define H_MSVC_DECLSPEC(x) +#endif + +#endif diff --git a/src/hammer.c b/src/hammer.c index 6bb9ebb4febe53668a91ae9617ba05f2c158023d..443c77b790b10b5592958a55e7d457bc695c030a 100644 --- a/src/hammer.c +++ b/src/hammer.c @@ -17,7 +17,6 @@ #include <assert.h> #include <ctype.h> -#include <err.h> #include <limits.h> #include <stdarg.h> #include <string.h> diff --git a/src/hammer.h b/src/hammer.h index 95f8c981401f02d29c27926e784058ac44162d4d..42c73458a4d0e513f4400e1a3c6790e9cc736a9e 100644 --- a/src/hammer.h +++ b/src/hammer.h @@ -18,11 +18,7 @@ #ifndef HAMMER_HAMMER__H #define HAMMER_HAMMER__H -#if defined(__clang__) || defined(__GNUC__) -#define H_GCC_ATTRIBUTE(x) __attribute__(x) -#else -#define H_GCC_ATTRIBUTE(x) -#endif +#include "compiler_specifics.h" #ifndef HAMMER_INTERNAL__NO_STDARG_H #include <stdarg.h> diff --git a/src/internal.h b/src/internal.h index ed1bd0856febf3fa422a471a21051d75d33d5297..9aac4ee7dbaa4c4a1b8e87785b98f22265f37c71 100644 --- a/src/internal.h +++ b/src/internal.h @@ -24,9 +24,9 @@ #define HAMMER_INTERNAL__H #include <stdint.h> #include <assert.h> -#include <err.h> #include <string.h> #include "hammer.h" +#include "platform.h" /* "Internal" in this case means "we're not ready to commit * to a public API." Many structures and routines here will be @@ -38,7 +38,7 @@ #else #define assert_message(check, message) do { \ if (!(check)) \ - errx(1, "Assertion failed (programmer error): %s", message); \ + h_platform_errx(1, "Assertion failed (programmer error): %s", message); \ } while(0) #endif diff --git a/src/parsers/many.c b/src/parsers/many.c index 1e3b0221ceae76c782a317e1c5c17b21a496f4a1..51d733fcf87e3191e6f413a9513ac7900d29d8f2 100644 --- a/src/parsers/many.c +++ b/src/parsers/many.c @@ -246,7 +246,7 @@ static HParseResult* parse_length_value(void *env, HParseState *state) { if (!len) return NULL; if (len->ast->token_type != TT_UINT) - errx(1, "Length parser must return an unsigned integer"); + h_platform_errx(1, "Length parser must return an unsigned integer"); // TODO: allocate this using public functions HRepeat repeat = { .p = lv->value, diff --git a/src/platform.h b/src/platform.h new file mode 100644 index 0000000000000000000000000000000000000000..0c05bfe2e7ada4e7f1c5f48c3d705a839a78730a --- /dev/null +++ b/src/platform.h @@ -0,0 +1,18 @@ +#ifndef HAMMER_PLATFORM__H +#define HAMMER_PLATFORM__H + +/** + * @file interface between hammer and the operating system / + * underlying platform. + */ + +#include "compiler_specifics.h" + +/* Error Reporting */ + +/* BSD errx function, seen in err.h */ +H_MSVC_DECLSPEC(noreturn) \ +void h_platform_errx(int err, const char* format, ...) \ + H_GCC_ATTRIBUTE((noreturn, format (printf,2,3))); + +#endif diff --git a/src/platform_bsdlike.c b/src/platform_bsdlike.c new file mode 100644 index 0000000000000000000000000000000000000000..ebb38d9df55243c75774dbfd9789932ee8d9c650 --- /dev/null +++ b/src/platform_bsdlike.c @@ -0,0 +1,10 @@ +#include "platform.h" + +#include <err.h> +#include <stdarg.h> + +void h_platform_errx(int err, const char* format, ...) { + va_list ap; + va_start(ap, format); + verrx(err, format, ap); +} diff --git a/src/platform_win32.c b/src/platform_win32.c new file mode 100644 index 0000000000000000000000000000000000000000..30af168170933456eb64370bd3cfd632b0558b50 --- /dev/null +++ b/src/platform_win32.c @@ -0,0 +1,10 @@ +#include "platform.h" + +#define WIN32_LEAN_AND_MEAN +#include "windows.h" + +void h_platform_errx(int err, const char* format, ...) { + // FIXME(windows) TODO(uucidl): to be implemented + ExitProcess(err); +} +