3 from clang.cindex import CursorKind
4 from clang.cindex import TranslationUnit
5 from clang.cindex import TypeKind
6 from nose.tools import raises
7 from .util import get_cursor
8 from .util import get_tu
30 teststruct = get_cursor(tu, 'teststruct')
31 assert teststruct is not None, "Could not find teststruct."
32 fields = list(teststruct.get_children())
33 assert all(x.kind == CursorKind.FIELD_DECL for x in fields)
34 assert all(x.translation_unit is not None for x in fields)
36 assert fields[0].spelling == 'a'
37 assert not fields[0].type.is_const_qualified()
38 assert fields[0].type.kind == TypeKind.INT
39 assert fields[0].type.get_canonical().kind == TypeKind.INT
41 assert fields[1].spelling == 'b'
42 assert not fields[1].type.is_const_qualified()
43 assert fields[1].type.kind == TypeKind.TYPEDEF
44 assert fields[1].type.get_canonical().kind == TypeKind.INT
45 assert fields[1].type.get_declaration().spelling == 'I'
47 assert fields[2].spelling == 'c'
48 assert not fields[2].type.is_const_qualified()
49 assert fields[2].type.kind == TypeKind.LONG
50 assert fields[2].type.get_canonical().kind == TypeKind.LONG
52 assert fields[3].spelling == 'd'
53 assert not fields[3].type.is_const_qualified()
54 assert fields[3].type.kind == TypeKind.ULONG
55 assert fields[3].type.get_canonical().kind == TypeKind.ULONG
57 assert fields[4].spelling == 'e'
58 assert not fields[4].type.is_const_qualified()
59 assert fields[4].type.kind == TypeKind.LONG
60 assert fields[4].type.get_canonical().kind == TypeKind.LONG
62 assert fields[5].spelling == 'f'
63 assert fields[5].type.is_const_qualified()
64 assert fields[5].type.kind == TypeKind.INT
65 assert fields[5].type.get_canonical().kind == TypeKind.INT
67 assert fields[6].spelling == 'g'
68 assert not fields[6].type.is_const_qualified()
69 assert fields[6].type.kind == TypeKind.POINTER
70 assert fields[6].type.get_pointee().kind == TypeKind.INT
72 assert fields[7].spelling == 'h'
73 assert not fields[7].type.is_const_qualified()
74 assert fields[7].type.kind == TypeKind.POINTER
75 assert fields[7].type.get_pointee().kind == TypeKind.POINTER
76 assert fields[7].type.get_pointee().get_pointee().kind == TypeKind.POINTER
77 assert fields[7].type.get_pointee().get_pointee().get_pointee().kind == TypeKind.INT
79 def test_references():
80 """Ensure that a Type maintains a reference to a TranslationUnit."""
83 children = list(tu.cursor.get_children())
84 assert len(children) > 0
89 assert isinstance(t.translation_unit, TranslationUnit)
91 # Delete main TranslationUnit reference and force a GC.
94 assert isinstance(t.translation_unit, TranslationUnit)
96 # If the TU was destroyed, this should cause a segfault.
97 decl = t.get_declaration()
104 def testConstantArray():
105 tu = get_tu(constarrayInput)
107 teststruct = get_cursor(tu, 'teststruct')
108 assert teststruct is not None, "Didn't find teststruct??"
109 fields = list(teststruct.get_children())
110 assert fields[0].spelling == 'A'
111 assert fields[0].type.kind == TypeKind.CONSTANTARRAY
112 assert fields[0].type.get_array_element_type() is not None
113 assert fields[0].type.get_array_element_type().kind == TypeKind.POINTER
114 assert fields[0].type.get_array_size() == 2
117 """Ensure equivalence operators work on Type."""
118 source = 'int a; int b; void *v;'
121 a = get_cursor(tu, 'a')
122 b = get_cursor(tu, 'b')
123 v = get_cursor(tu, 'v')
129 assert a.type == b.type
130 assert a.type != v.type
132 assert a.type != None
133 assert a.type != 'foo'
135 def test_type_spelling():
136 """Ensure Type.spelling works."""
137 tu = get_tu('int c[5]; int i[]; int x; int v[x];')
138 c = get_cursor(tu, 'c')
139 i = get_cursor(tu, 'i')
140 x = get_cursor(tu, 'x')
141 v = get_cursor(tu, 'v')
146 assert c.type.spelling == "int [5]"
147 assert i.type.spelling == "int []"
148 assert x.type.spelling == "int"
149 assert v.type.spelling == "int [x]"
151 def test_typekind_spelling():
152 """Ensure TypeKind.spelling works."""
153 tu = get_tu('int a;')
154 a = get_cursor(tu, 'a')
157 assert a.type.kind.spelling == 'Int'
159 def test_function_argument_types():
160 """Ensure that Type.argument_types() works as expected."""
161 tu = get_tu('void f(int, int);')
162 f = get_cursor(tu, 'f')
165 args = f.type.argument_types()
166 assert args is not None
167 assert len(args) == 2
170 assert t0 is not None
171 assert t0.kind == TypeKind.INT
174 assert t1 is not None
175 assert t1.kind == TypeKind.INT
178 assert len(args2) == 2
179 assert t0 == args2[0]
180 assert t1 == args2[1]
183 def test_argument_types_string_key():
184 """Ensure that non-int keys raise a TypeError."""
185 tu = get_tu('void f(int, int);')
186 f = get_cursor(tu, 'f')
189 args = f.type.argument_types()
190 assert len(args) == 2
195 def test_argument_types_negative_index():
196 """Ensure that negative indexes on argument_types Raises an IndexError."""
197 tu = get_tu('void f(int, int);')
198 f = get_cursor(tu, 'f')
199 args = f.type.argument_types()
204 def test_argument_types_overflow_index():
205 """Ensure that indexes beyond the length of Type.argument_types() raise."""
206 tu = get_tu('void f(int, int);')
207 f = get_cursor(tu, 'f')
208 args = f.type.argument_types()
213 def test_argument_types_invalid_type():
214 """Ensure that obtaining argument_types on a Type without them raises."""
215 tu = get_tu('int i;')
216 i = get_cursor(tu, 'i')
219 i.type.argument_types()
222 """Ensure Type.is_pod() works."""
223 tu = get_tu('int i; void f();')
224 i = get_cursor(tu, 'i')
225 f = get_cursor(tu, 'f')
230 assert i.type.is_pod()
231 assert not f.type.is_pod()
233 def test_function_variadic():
234 """Ensure Type.is_function_variadic works."""
239 void foo(int a, ...);
240 void bar(int a, int b);
244 foo = get_cursor(tu, 'foo')
245 bar = get_cursor(tu, 'bar')
247 assert foo is not None
248 assert bar is not None
250 assert isinstance(foo.type.is_function_variadic(), bool)
251 assert foo.type.is_function_variadic()
252 assert not bar.type.is_function_variadic()
254 def test_element_type():
255 """Ensure Type.element_type works."""
256 tu = get_tu('int c[5]; int i[]; int x; int v[x];')
257 c = get_cursor(tu, 'c')
258 i = get_cursor(tu, 'i')
259 v = get_cursor(tu, 'v')
264 assert c.type.kind == TypeKind.CONSTANTARRAY
265 assert c.type.element_type.kind == TypeKind.INT
266 assert i.type.kind == TypeKind.INCOMPLETEARRAY
267 assert i.type.element_type.kind == TypeKind.INT
268 assert v.type.kind == TypeKind.VARIABLEARRAY
269 assert v.type.element_type.kind == TypeKind.INT
272 def test_invalid_element_type():
273 """Ensure Type.element_type raises if type doesn't have elements."""
274 tu = get_tu('int i;')
275 i = get_cursor(tu, 'i')
279 def test_element_count():
280 """Ensure Type.element_count works."""
281 tu = get_tu('int i[5]; int j;')
282 i = get_cursor(tu, 'i')
283 j = get_cursor(tu, 'j')
288 assert i.type.element_count == 5
296 def test_is_volatile_qualified():
297 """Ensure Type.is_volatile_qualified works."""
299 tu = get_tu('volatile int i = 4; int j = 2;')
301 i = get_cursor(tu, 'i')
302 j = get_cursor(tu, 'j')
307 assert isinstance(i.type.is_volatile_qualified(), bool)
308 assert i.type.is_volatile_qualified()
309 assert not j.type.is_volatile_qualified()
311 def test_is_restrict_qualified():
312 """Ensure Type.is_restrict_qualified works."""
314 tu = get_tu('struct s { void * restrict i; void * j; };')
316 i = get_cursor(tu, 'i')
317 j = get_cursor(tu, 'j')
322 assert isinstance(i.type.is_restrict_qualified(), bool)
323 assert i.type.is_restrict_qualified()
324 assert not j.type.is_restrict_qualified()
326 def test_record_layout():
327 """Ensure Cursor.type.get_size, Cursor.type.get_align and
328 Cursor.type.get_offset works."""
338 tries=[(['-target','i386-linux-gnu'],(4,16,0,32,35,64)),
339 (['-target','nvptx64-unknown-unknown'],(8,24,0,64,67,128)),
340 (['-target','i386-pc-win32'],(8,16,0,32,35,64)),
341 (['-target','msp430-none-none'],(2,14,0,32,35,48))]
342 for flags, values in tries:
343 align,total,a1,a2,a3,a4 = values
345 tu = get_tu(source, flags=flags)
346 teststruct = get_cursor(tu, 'a')
347 fields = list(teststruct.get_children())
349 assert teststruct.type.get_align() == align
350 assert teststruct.type.get_size() == total
351 assert teststruct.type.get_offset(fields[0].spelling) == a1
352 assert teststruct.type.get_offset(fields[1].spelling) == a2
353 assert teststruct.type.get_offset(fields[2].spelling) == a3
354 assert teststruct.type.get_offset(fields[3].spelling) == a4
355 assert fields[0].is_bitfield() == False
356 assert fields[1].is_bitfield() == True
357 assert fields[1].get_bitfield_width() == 3
358 assert fields[2].is_bitfield() == True
359 assert fields[2].get_bitfield_width() == 4
360 assert fields[3].is_bitfield() == False
363 """Ensure Cursor.get_record_field_offset works in anonymous records"""
374 tries=[(['-target','i386-linux-gnu'],(4,16,0,32,64)),
375 (['-target','nvptx64-unknown-unknown'],(8,24,0,32,64)),
376 (['-target','i386-pc-win32'],(8,16,0,32,64)),
377 (['-target','msp430-none-none'],(2,14,0,32,64))]
378 for flags, values in tries:
379 align,total,bariton,foo,bar = values
381 teststruct = get_cursor(tu, 'Test')
382 fields = list(teststruct.get_children())
383 assert teststruct.type.get_offset("bariton") == bariton
384 assert teststruct.type.get_offset("foo") == foo
385 assert teststruct.type.get_offset("bar") == bar
389 """Ensure decayed types are handled as the original type"""
391 tu = get_tu("void foo(int a[]);")
392 foo = get_cursor(tu, 'foo')
393 a = foo.type.argument_types()[0]
395 assert a.kind == TypeKind.INCOMPLETEARRAY
396 assert a.element_type.kind == TypeKind.INT
397 assert a.get_canonical().kind == TypeKind.INCOMPLETEARRAY