import gc
from mlir.ir import *
def run(f):
print("\nTEST:", f.__name__)
f()
gc.collect()
assert Context._get_live_count() == 0
def testUnknown():
with Context() as ctx:
loc = Location.unknown()
assert loc.context is ctx
ctx = None
gc.collect()
print("unknown str:", str(loc))
print("unknown repr:", repr(loc))
run(testUnknown)
def testLocationAttr():
with Context() as ctxt:
loc = Location.unknown()
attr = loc.attr
clone = Location.from_attr(attr)
gc.collect()
print("loc:", str(loc))
print("clone:", str(clone))
assert loc == clone
run(testLocationAttr)
def testFileLineCol():
with Context() as ctx:
loc = Location.file("foo.txt", 123, 56)
ctx = None
gc.collect()
print("file str:", str(loc))
print("file repr:", repr(loc))
run(testFileLineCol)
def testName():
with Context() as ctx:
loc = Location.name("nombre")
locWithChildLoc = Location.name("naam", loc)
ctx = None
gc.collect()
print("file str:", str(loc))
print("file repr:", repr(loc))
print("file str:", str(locWithChildLoc))
print("file repr:", repr(locWithChildLoc))
run(testName)
def testCallSite():
with Context() as ctx:
loc = Location.callsite(
Location.file("foo.text", 123, 45),
[Location.file("util.foo", 379, 21), Location.file("main.foo", 100, 63)],
)
ctx = None
print("file str:", str(loc))
print("file repr:", repr(loc))
run(testCallSite)
def testFused():
with Context() as ctx:
loc_single = Location.fused([Location.name("apple")])
loc = Location.fused([Location.name("apple"), Location.name("banana")])
attr = Attribute.parse('"sauteed"')
loc_attr = Location.fused(
[Location.name("carrot"), Location.name("potatoes")], attr
)
loc_empty = Location.fused([])
loc_empty_attr = Location.fused([], attr)
loc_single_attr = Location.fused([Location.name("apple")], attr)
ctx = None
print("file str:", str(loc_single))
print("file repr:", repr(loc_single))
print("file str:", str(loc))
print("file repr:", repr(loc))
print("file str:", str(loc_attr))
print("file repr:", repr(loc_attr))
print("file str:", str(loc_empty))
print("file repr:", repr(loc_empty))
print("file str:", str(loc_empty_attr))
print("file repr:", repr(loc_empty_attr))
print("file str:", str(loc_single_attr))
print("file repr:", repr(loc_single_attr))
run(testFused)
def testLocationCapsule():
with Context() as ctx:
loc1 = Location.file("foo.txt", 123, 56)
loc_capsule = loc1._CAPIPtr
print(loc_capsule)
loc2 = Location._CAPICreate(loc_capsule)
assert loc2 == loc1
assert loc2.context is ctx
run(testLocationCapsule)