1# RUN: %PYTHON %s | FileCheck %s 2 3import gc 4from mlir.ir import * 5 6def run(f): 7 print("\nTEST:", f.__name__) 8 f() 9 gc.collect() 10 assert Context._get_live_count() == 0 11 12 13# CHECK-LABEL: TEST: testUnknown 14def testUnknown(): 15 with Context() as ctx: 16 loc = Location.unknown() 17 assert loc.context is ctx 18 ctx = None 19 gc.collect() 20 # CHECK: unknown str: loc(unknown) 21 print("unknown str:", str(loc)) 22 # CHECK: unknown repr: loc(unknown) 23 print("unknown repr:", repr(loc)) 24 25run(testUnknown) 26 27 28# CHECK-LABEL: TEST: testFileLineCol 29def testFileLineCol(): 30 with Context() as ctx: 31 loc = Location.file("foo.txt", 123, 56) 32 ctx = None 33 gc.collect() 34 # CHECK: file str: loc("foo.txt":123:56) 35 print("file str:", str(loc)) 36 # CHECK: file repr: loc("foo.txt":123:56) 37 print("file repr:", repr(loc)) 38 39run(testFileLineCol) 40 41 42# CHECK-LABEL: TEST: testName 43def testName(): 44 with Context() as ctx: 45 loc = Location.name("nombre") 46 locWithChildLoc = Location.name("naam", loc) 47 ctx = None 48 gc.collect() 49 # CHECK: file str: loc("nombre") 50 print("file str:", str(loc)) 51 # CHECK: file repr: loc("nombre") 52 print("file repr:", repr(loc)) 53 # CHECK: file str: loc("naam"("nombre")) 54 print("file str:", str(locWithChildLoc)) 55 # CHECK: file repr: loc("naam"("nombre")) 56 print("file repr:", repr(locWithChildLoc)) 57 58run(testName) 59 60 61# CHECK-LABEL: TEST: testCallSite 62def testCallSite(): 63 with Context() as ctx: 64 loc = Location.callsite( 65 Location.file("foo.text", 123, 45), [ 66 Location.file("util.foo", 379, 21), 67 Location.file("main.foo", 100, 63) 68 ]) 69 ctx = None 70 # CHECK: file str: loc(callsite("foo.text":123:45 at callsite("util.foo":379:21 at "main.foo":100:63)) 71 print("file str:", str(loc)) 72 # CHECK: file repr: loc(callsite("foo.text":123:45 at callsite("util.foo":379:21 at "main.foo":100:63)) 73 print("file repr:", repr(loc)) 74 75run(testCallSite) 76 77 78# CHECK-LABEL: TEST: testLocationCapsule 79def testLocationCapsule(): 80 with Context() as ctx: 81 loc1 = Location.file("foo.txt", 123, 56) 82 # CHECK: mlir.ir.Location._CAPIPtr 83 loc_capsule = loc1._CAPIPtr 84 print(loc_capsule) 85 loc2 = Location._CAPICreate(loc_capsule) 86 assert loc2 == loc1 87 assert loc2.context is ctx 88 89run(testLocationCapsule) 90