From be1e3c4e09e4c5592428e71d875ef6a623c82804 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Thu, 3 Nov 2016 21:11:39 +0800 Subject: add a simple sizeof checker to compare nim & c types --- lib/nimbase.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib/nimbase.h') diff --git a/lib/nimbase.h b/lib/nimbase.h index 52de60969..a75016ed7 100644 --- a/lib/nimbase.h +++ b/lib/nimbase.h @@ -459,3 +459,11 @@ typedef int Nim_and_C_compiler_disagree_on_target_architecture[sizeof(NI) == siz #elif defined(__FreeBSD__) # include #endif + +/* Compile with -t:-DNIM_CHECK_ABI to enable */ +#ifdef NIM_CHECK_ABI +# define NIM_CHECK_SIZE(typ, sz) \ + _Static_assert(sizeof(typ) == sz, "Nim & C disagree on type size") +#else +# define NIM_CHECK_SIZE(typ, sz) +#endif -- cgit 1.4.1-2-gfad0 From fa86571448ea89143ca78576c8248bc02ad056bd Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Thu, 3 Nov 2016 22:30:00 +0800 Subject: abi check: prefer nim constant to enable, document --- compiler/ccgtypes.nim | 2 +- doc/nimc.rst | 4 ++++ lib/nimbase.h | 8 ++------ 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'lib/nimbase.h') diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim index 0327d9b82..a9c27217a 100644 --- a/compiler/ccgtypes.nim +++ b/compiler/ccgtypes.nim @@ -703,7 +703,7 @@ proc getTypeDescAux(m: BModule, typ: PType, check: var IntSet): Rope = else: getTupleDesc(m, t, result, check) if not isImportedType(t): add(m.s[cfsTypes], recdesc) - elif tfIncompleteStruct notin t.flags: + elif tfIncompleteStruct notin t.flags and isDefined("checkabi"): addf(m.s[cfsTypeInfo], "NIM_CHECK_SIZE($1, $2);$n", [result, rope(getSize(t))]) of tySet: result = getTypeName(t.lastSon) & "Set" diff --git a/doc/nimc.rst b/doc/nimc.rst index eb1beb549..5d9ed03ab 100644 --- a/doc/nimc.rst +++ b/doc/nimc.rst @@ -258,6 +258,10 @@ Define Effect ``ssl`` Enables OpenSSL support for the sockets module. ``memProfiler`` Enables memory profiling for the native GC. ``uClibc`` Use uClibc instead of libc. (Relevant for Unix-like OSes) +``checkAbi`` When using types from C headers, add checks that compare + what's in the Nim file with what's in the C header + (requires a C compiler with _Static_assert support, like + any C11 compiler) ================== ========================================================= diff --git a/lib/nimbase.h b/lib/nimbase.h index a75016ed7..818bff462 100644 --- a/lib/nimbase.h +++ b/lib/nimbase.h @@ -460,10 +460,6 @@ typedef int Nim_and_C_compiler_disagree_on_target_architecture[sizeof(NI) == siz # include #endif -/* Compile with -t:-DNIM_CHECK_ABI to enable */ -#ifdef NIM_CHECK_ABI -# define NIM_CHECK_SIZE(typ, sz) \ +/* Compile with -d:checkAbi and a sufficiently C11:ish compiler to enable */ +#define NIM_CHECK_SIZE(typ, sz) \ _Static_assert(sizeof(typ) == sz, "Nim & C disagree on type size") -#else -# define NIM_CHECK_SIZE(typ, sz) -#endif -- cgit 1.4.1-2-gfad0