[clang-tidy][NFC]remove deps of clang in clang tidy test (#116588)
[llvm-project.git] / mlir / test / python / ir / location.py
blobf66d6c501dcf5c6fcf0e6b251465980067c3f67f
1 # RUN: %PYTHON %s | FileCheck %s
3 import gc
4 from mlir.ir import *
7 def run(f):
8 print("\nTEST:", f.__name__)
9 f()
10 gc.collect()
11 assert Context._get_live_count() == 0
14 # CHECK-LABEL: TEST: testUnknown
15 def testUnknown():
16 with Context() as ctx:
17 loc = Location.unknown()
18 assert loc.context is ctx
19 ctx = None
20 gc.collect()
21 # CHECK: unknown str: loc(unknown)
22 print("unknown str:", str(loc))
23 # CHECK: unknown repr: loc(unknown)
24 print("unknown repr:", repr(loc))
27 run(testUnknown)
30 # CHECK-LABEL: TEST: testLocationAttr
31 def testLocationAttr():
32 with Context() as ctxt:
33 loc = Location.unknown()
34 attr = loc.attr
35 clone = Location.from_attr(attr)
36 gc.collect()
37 # CHECK: loc: loc(unknown)
38 print("loc:", str(loc))
39 # CHECK: clone: loc(unknown)
40 print("clone:", str(clone))
41 assert loc == clone
44 run(testLocationAttr)
46 # CHECK-LABEL: TEST: testFileLineCol
47 def testFileLineCol():
48 with Context() as ctx:
49 loc = Location.file("foo.txt", 123, 56)
50 ctx = None
51 gc.collect()
52 # CHECK: file str: loc("foo.txt":123:56)
53 print("file str:", str(loc))
54 # CHECK: file repr: loc("foo.txt":123:56)
55 print("file repr:", repr(loc))
58 run(testFileLineCol)
61 # CHECK-LABEL: TEST: testName
62 def testName():
63 with Context() as ctx:
64 loc = Location.name("nombre")
65 locWithChildLoc = Location.name("naam", loc)
66 ctx = None
67 gc.collect()
68 # CHECK: file str: loc("nombre")
69 print("file str:", str(loc))
70 # CHECK: file repr: loc("nombre")
71 print("file repr:", repr(loc))
72 # CHECK: file str: loc("naam"("nombre"))
73 print("file str:", str(locWithChildLoc))
74 # CHECK: file repr: loc("naam"("nombre"))
75 print("file repr:", repr(locWithChildLoc))
78 run(testName)
81 # CHECK-LABEL: TEST: testCallSite
82 def testCallSite():
83 with Context() as ctx:
84 loc = Location.callsite(
85 Location.file("foo.text", 123, 45),
86 [Location.file("util.foo", 379, 21), Location.file("main.foo", 100, 63)],
88 ctx = None
89 # CHECK: file str: loc(callsite("foo.text":123:45 at callsite("util.foo":379:21 at "main.foo":100:63))
90 print("file str:", str(loc))
91 # CHECK: file repr: loc(callsite("foo.text":123:45 at callsite("util.foo":379:21 at "main.foo":100:63))
92 print("file repr:", repr(loc))
95 run(testCallSite)
98 # CHECK-LABEL: TEST: testFused
99 def testFused():
100 with Context() as ctx:
101 loc_single = Location.fused([Location.name("apple")])
102 loc = Location.fused([Location.name("apple"), Location.name("banana")])
103 attr = Attribute.parse('"sauteed"')
104 loc_attr = Location.fused(
105 [Location.name("carrot"), Location.name("potatoes")], attr
107 loc_empty = Location.fused([])
108 loc_empty_attr = Location.fused([], attr)
109 loc_single_attr = Location.fused([Location.name("apple")], attr)
110 ctx = None
111 # CHECK: file str: loc("apple")
112 print("file str:", str(loc_single))
113 # CHECK: file repr: loc("apple")
114 print("file repr:", repr(loc_single))
115 # CHECK: file str: loc(fused["apple", "banana"])
116 print("file str:", str(loc))
117 # CHECK: file repr: loc(fused["apple", "banana"])
118 print("file repr:", repr(loc))
119 # CHECK: file str: loc(fused<"sauteed">["carrot", "potatoes"])
120 print("file str:", str(loc_attr))
121 # CHECK: file repr: loc(fused<"sauteed">["carrot", "potatoes"])
122 print("file repr:", repr(loc_attr))
123 # CHECK: file str: loc(unknown)
124 print("file str:", str(loc_empty))
125 # CHECK: file repr: loc(unknown)
126 print("file repr:", repr(loc_empty))
127 # CHECK: file str: loc(fused<"sauteed">[unknown])
128 print("file str:", str(loc_empty_attr))
129 # CHECK: file repr: loc(fused<"sauteed">[unknown])
130 print("file repr:", repr(loc_empty_attr))
131 # CHECK: file str: loc(fused<"sauteed">["apple"])
132 print("file str:", str(loc_single_attr))
133 # CHECK: file repr: loc(fused<"sauteed">["apple"])
134 print("file repr:", repr(loc_single_attr))
137 run(testFused)
140 # CHECK-LABEL: TEST: testLocationCapsule
141 def testLocationCapsule():
142 with Context() as ctx:
143 loc1 = Location.file("foo.txt", 123, 56)
144 # CHECK: mlir.ir.Location._CAPIPtr
145 loc_capsule = loc1._CAPIPtr
146 print(loc_capsule)
147 loc2 = Location._CAPICreate(loc_capsule)
148 assert loc2 == loc1
149 assert loc2.context is ctx
152 run(testLocationCapsule)