Merge pull request #4994 from arnetheduck/nim-check-abi
add a simple sizeof checker to compare nim & c types
This commit is contained in:
commit
86b58e83ea
3 changed files with 21 additions and 1 deletions
|
|
@ -206,6 +206,10 @@ proc cacheGetType(tab: TIdTable, key: PType): Rope =
|
||||||
# linear search is not necessary anymore:
|
# linear search is not necessary anymore:
|
||||||
result = Rope(idTableGet(tab, key))
|
result = Rope(idTableGet(tab, key))
|
||||||
|
|
||||||
|
proc addAbiCheck(m: BModule, t: PType, name: Rope) =
|
||||||
|
if isDefined("checkabi"):
|
||||||
|
addf(m.s[cfsTypeInfo], "NIM_CHECK_SIZE($1, $2);$n", [name, rope(getSize(t))])
|
||||||
|
|
||||||
proc getTempName(m: BModule): Rope =
|
proc getTempName(m: BModule): Rope =
|
||||||
result = m.tmpBase & rope(m.labels)
|
result = m.tmpBase & rope(m.labels)
|
||||||
inc m.labels
|
inc m.labels
|
||||||
|
|
@ -267,6 +271,11 @@ proc getSimpleTypeDesc(m: BModule, typ: PType): Rope =
|
||||||
result = getSimpleTypeDesc(m, lastSon typ)
|
result = getSimpleTypeDesc(m, lastSon typ)
|
||||||
else: result = nil
|
else: result = nil
|
||||||
|
|
||||||
|
if result != nil and typ.isImportedType():
|
||||||
|
if cacheGetType(m.typeCache, typ) == nil:
|
||||||
|
idTablePut(m.typeCache, typ, result)
|
||||||
|
addAbiCheck(m, typ, result)
|
||||||
|
|
||||||
proc pushType(m: BModule, typ: PType) =
|
proc pushType(m: BModule, typ: PType) =
|
||||||
add(m.typeStack, typ)
|
add(m.typeStack, typ)
|
||||||
|
|
||||||
|
|
@ -656,6 +665,7 @@ proc getTypeDescAux(m: BModule, typ: PType, check: var IntSet): Rope =
|
||||||
let foo = getTypeDescAux(m, t.sons[1], check)
|
let foo = getTypeDescAux(m, t.sons[1], check)
|
||||||
addf(m.s[cfsTypes], "typedef $1 $2[$3];$n",
|
addf(m.s[cfsTypes], "typedef $1 $2[$3];$n",
|
||||||
[foo, result, rope(n)])
|
[foo, result, rope(n)])
|
||||||
|
else: addAbiCheck(m, t, result)
|
||||||
of tyObject, tyTuple:
|
of tyObject, tyTuple:
|
||||||
if isImportedCppType(t) and typ.kind == tyGenericInst:
|
if isImportedCppType(t) and typ.kind == tyGenericInst:
|
||||||
# for instantiated templates we do not go through the type cache as the
|
# for instantiated templates we do not go through the type cache as the
|
||||||
|
|
@ -701,7 +711,9 @@ proc getTypeDescAux(m: BModule, typ: PType, check: var IntSet): Rope =
|
||||||
idTablePut(m.typeCache, t, result) # always call for sideeffects:
|
idTablePut(m.typeCache, t, result) # always call for sideeffects:
|
||||||
let recdesc = if t.kind != tyTuple: getRecordDesc(m, t, result, check)
|
let recdesc = if t.kind != tyTuple: getRecordDesc(m, t, result, check)
|
||||||
else: getTupleDesc(m, t, result, check)
|
else: getTupleDesc(m, t, result, check)
|
||||||
if not isImportedType(t): add(m.s[cfsTypes], recdesc)
|
if not isImportedType(t):
|
||||||
|
add(m.s[cfsTypes], recdesc)
|
||||||
|
elif tfIncompleteStruct notin t.flags: addAbiCheck(m, t, result)
|
||||||
of tySet:
|
of tySet:
|
||||||
result = getTypeName(t.lastSon) & "Set"
|
result = getTypeName(t.lastSon) & "Set"
|
||||||
idTablePut(m.typeCache, t, result)
|
idTablePut(m.typeCache, t, result)
|
||||||
|
|
|
||||||
|
|
@ -258,6 +258,10 @@ Define Effect
|
||||||
``ssl`` Enables OpenSSL support for the sockets module.
|
``ssl`` Enables OpenSSL support for the sockets module.
|
||||||
``memProfiler`` Enables memory profiling for the native GC.
|
``memProfiler`` Enables memory profiling for the native GC.
|
||||||
``uClibc`` Use uClibc instead of libc. (Relevant for Unix-like OSes)
|
``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)
|
||||||
================== =========================================================
|
================== =========================================================
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -459,3 +459,7 @@ typedef int Nim_and_C_compiler_disagree_on_target_architecture[sizeof(NI) == siz
|
||||||
#elif defined(__FreeBSD__)
|
#elif defined(__FreeBSD__)
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* 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")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue