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: testFused
79def testFused():
80  with Context() as ctx:
81    loc_single = Location.fused([Location.name("apple")])
82    loc = Location.fused(
83        [Location.name("apple"), Location.name("banana")])
84    attr = Attribute.parse('"sauteed"')
85    loc_attr = Location.fused([Location.name("carrot"),
86                               Location.name("potatoes")], attr)
87    loc_empty = Location.fused([])
88    loc_empty_attr = Location.fused([], attr)
89    loc_single_attr = Location.fused([Location.name("apple")], attr)
90  ctx = None
91  # CHECK: file str: loc("apple")
92  print("file str:", str(loc_single))
93  # CHECK: file repr: loc("apple")
94  print("file repr:", repr(loc_single))
95  # CHECK: file str: loc(fused["apple", "banana"])
96  print("file str:", str(loc))
97  # CHECK: file repr: loc(fused["apple", "banana"])
98  print("file repr:", repr(loc))
99  # CHECK: file str: loc(fused<"sauteed">["carrot", "potatoes"])
100  print("file str:", str(loc_attr))
101  # CHECK: file repr: loc(fused<"sauteed">["carrot", "potatoes"])
102  print("file repr:", repr(loc_attr))
103  # CHECK: file str: loc(unknown)
104  print("file str:", str(loc_empty))
105  # CHECK: file repr: loc(unknown)
106  print("file repr:", repr(loc_empty))
107  # CHECK: file str: loc(fused<"sauteed">[unknown])
108  print("file str:", str(loc_empty_attr))
109  # CHECK: file repr: loc(fused<"sauteed">[unknown])
110  print("file repr:", repr(loc_empty_attr))
111  # CHECK: file str: loc(fused<"sauteed">["apple"])
112  print("file str:", str(loc_single_attr))
113  # CHECK: file repr: loc(fused<"sauteed">["apple"])
114  print("file repr:", repr(loc_single_attr))
115
116run(testFused)
117
118
119# CHECK-LABEL: TEST: testLocationCapsule
120def testLocationCapsule():
121  with Context() as ctx:
122    loc1 = Location.file("foo.txt", 123, 56)
123  # CHECK: mlir.ir.Location._CAPIPtr
124  loc_capsule = loc1._CAPIPtr
125  print(loc_capsule)
126  loc2 = Location._CAPICreate(loc_capsule)
127  assert loc2 == loc1
128  assert loc2.context is ctx
129
130run(testLocationCapsule)
131