Skip to content

Commit

Permalink
mimportc_size_check.nim => msizeof5.nim; merge mabi_check.nim into ms…
Browse files Browse the repository at this point in the history
…izeof5.nim; refactor
  • Loading branch information
timotheecour committed Apr 10, 2020
1 parent 172a03a commit 689aec6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
7 changes: 0 additions & 7 deletions tests/ccgbugs/mabi_check.nim

This file was deleted.

15 changes: 13 additions & 2 deletions tests/ccgbugs/mimportc_size_check.nim → tests/misc/msizeof5.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## tests for addAbiCheck (via NIM_STATIC_ASSERT)
## tests for -d:checkAbi used by addAbiCheck via NIM_STATIC_ASSERT

{.emit:"""
struct Foo1{
Expand Down Expand Up @@ -83,14 +83,25 @@ block: # CT sizeof
static: doAssert (Foo6, int, array[2, Foo6]).sizeof ==
(Foo6GT, int, array[2, Foo6GT]).sizeof

block:
type GoodImportcType {.importc: "signed char", nodecl.} = char
# "good" in sense the sizeof will match
ensureCgen GoodImportcType

block:
type Foo6{.importc.} = object
a1: cint
doAssert compiles(Foo6.sizeof)
static: doAssert not compiles(static(Foo6.sizeof))

when defined caseBad:
# bad sizes => each should give an assert fail message
# Each case below should give a static cgen assert fail message

block:
type BadImportcType {.importc: "unsigned char", nodecl.} = uint64
# "sizeof" check will fail
ensureCgen BadImportcType

block:
type Foo2AliasBad{.importc: "struct Foo2", size: 1.} = object
a: cint
Expand Down
31 changes: 15 additions & 16 deletions tests/trunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,26 @@ ret={s1:foobar s2:foobar age:25 pi:3.14}
doAssert exitCode == 0

else: # don't run twice the same test
template check(msg) = doAssert msg in output, output

block: # mstatic_assert
let (output, exitCode) = runCmd("ccgbugs/mstatic_assert.nim", "-d:caseBad")
doAssert "sizeof(bool) == 2" in output, output
doAssert exitCode != 0

block: # mabi_check
let (output, exitCode) = runCmd("ccgbugs/mabi_check.nim")
# on platforms that support _StaticAssert natively, will show full context, eg:
# error: static_assert failed due to requirement 'sizeof(unsigned char) == 8'
# "backend & Nim disagree on size for: BadImportcType{int64} [declared in mabi_check.nim(1, 6)]"
doAssert "sizeof(unsigned char) == " in output, output
check "sizeof(bool) == 2"
doAssert exitCode != 0

block: # mimportc_size_check
block: # ABI checks
let file = "misc/msizeof5.nim"
block:
let (_, exitCode) = runCmd("ccgbugs/mimportc_size_check.nim")
let (_, exitCode) = runCmd(file, "-d:checkAbi")
doAssert exitCode == 0
block:
let (output, exitCode) = runCmd("ccgbugs/mimportc_size_check.nim", "-d:caseBad")
doAssert "sizeof(struct Foo2) == 1" in output, output
doAssert "sizeof(Foo5) == 16" in output, output
doAssert "sizeof(Foo5) == 3" in output, output
doAssert "sizeof(struct Foo6) == " in output, output
let (output, exitCode) = runCmd(file, "-d:checkAbi -d:caseBad")
# on platforms that support _StaticAssert natively, errors will show full context, eg:
# error: static_assert failed due to requirement 'sizeof(unsigned char) == 8'
# "backend & Nim disagree on size for: BadImportcType{int64} [declared in mabi_check.nim(1, 6)]"
check "sizeof(unsigned char) == 8"
check "sizeof(struct Foo2) == 1"
check "sizeof(Foo5) == 16"
check "sizeof(Foo5) == 3"
check "sizeof(struct Foo6) == "
doAssert exitCode != 0

0 comments on commit 689aec6

Please sign in to comment.