tsizeof test is now correct (#10788)

This commit is contained in:
Arne Döring 2019-03-06 08:12:16 +01:00 • committed by Andreas Rumpf
commit 2c01f0ad8d

View file

@ -1,5 +1,9 @@
discard """ discard """
output: "OK" output: '''
body executed
body executed
OK
'''
""" """
type type
@ -138,15 +142,41 @@ type
ValueA ValueA
ValueB ValueB
template testinstance(body: untyped): untyped =
block:
{.pragma: objectconfig.}
body
block: proc transformObjectconfigPacked(arg: NimNode): NimNode =
{.pragma: objectconfig, packed.} let debug = arg.kind == nnkPragmaExpr
body
if arg.eqIdent("objectconfig"):
result = ident"packed"
else:
result = copyNimNode(arg)
for child in arg:
result.add transformObjectconfigPacked(child)
proc removeObjectconfig(arg: NimNode): NimNode =
if arg.kind == nnkPragmaExpr and arg[1][0].eqIdent "objectconfig":
result = arg[0]
else:
result = copyNimNode(arg)
for child in arg:
result.add removeObjectconfig(child)
macro testinstance(body: untyped): untyped =
let bodyPure = removeObjectconfig(body)
let bodyPacked = transformObjectconfigPacked(body)
result = quote do:
proc pureblock(): void =
const usePacked {.inject.} = false
`bodyPure`
pureblock()
proc packedblock(): void =
const usePacked {.inject.} = true
`bodyPacked`
packedblock()
proc testPrimitiveTypes(): void = proc testPrimitiveTypes(): void =
testAlign(pointer) testAlign(pointer)
@ -284,13 +314,6 @@ testinstance:
a: int32 a: int32
b: T b: T
#Float128Test = object
# a: byte
# b: float128
#Bazang = object of RootObj
# a: float128
const trivialSize = sizeof(TrivialType) # needs to be able to evaluate at compile time const trivialSize = sizeof(TrivialType) # needs to be able to evaluate at compile time
proc main(): void = proc main(): void =
@ -314,9 +337,14 @@ testinstance:
eoa: EnumObjectA eoa: EnumObjectA
eob: EnumObjectB eob: EnumObjectB
testAlign(SimpleAlignment) testAlign(SimpleAlignment)
# sanity check to ensure both branches are actually executed
when usePacked:
doAssert sizeof(SimpleAlignment) == 10
else:
doAssert sizeof(SimpleAlignment) > 10
testSizeAlignOf(t,a,b,c,d,e,f,g,ro,go, e1, e2, e4, e8, eoa, eob) testSizeAlignOf(t,a,b,c,d,e,f,g,ro,go, e1, e2, e4, e8, eoa, eob)
when not defined(cpp): when not defined(cpp):
@ -380,6 +408,9 @@ testinstance:
testOffsetOf(RecursiveStuff, d1) testOffsetOf(RecursiveStuff, d1)
testOffsetOf(RecursiveStuff, d2) testOffsetOf(RecursiveStuff, d2)
echo "body executed" # sanity check to ensure this logic isn't skipped entirely
main() main()
{.emit: """/*TYPESECTION*/ {.emit: """/*TYPESECTION*/