test_type.py revision 13102ffbb00f1397fa02950e0cbc82d17be21792
1from clang.cindex import Index, CursorKind, TypeKind
2
3kInput = """\
4
5typedef int I;
6
7struct teststruct {
8  int a;
9  I b;
10  long c;
11  unsigned long d;
12  signed long e;
13  const int f;
14  int *g;
15  int ***h;
16};
17
18"""
19
20def test_a_struct():
21    index = Index.create()
22    tu = index.parse('t.c', unsaved_files = [('t.c',kInput)])
23
24    for n in tu.cursor.get_children():
25        if n.spelling == 'teststruct':
26            fields = list(n.get_children())
27
28            assert all(x.kind == CursorKind.FIELD_DECL for x in fields)
29
30            assert fields[0].spelling == 'a'
31            assert not fields[0].type.is_const_qualified()
32            assert fields[0].type.kind == TypeKind.INT
33            assert fields[0].type.get_canonical().kind == TypeKind.INT
34
35            assert fields[1].spelling == 'b'
36            assert not fields[1].type.is_const_qualified()
37            assert fields[1].type.kind == TypeKind.TYPEDEF
38            assert fields[1].type.get_canonical().kind == TypeKind.INT
39            assert fields[1].type.get_declaration().spelling == 'I'
40
41            assert fields[2].spelling == 'c'
42            assert not fields[2].type.is_const_qualified()
43            assert fields[2].type.kind == TypeKind.LONG
44            assert fields[2].type.get_canonical().kind == TypeKind.LONG
45
46            assert fields[3].spelling == 'd'
47            assert not fields[3].type.is_const_qualified()
48            assert fields[3].type.kind == TypeKind.ULONG
49            assert fields[3].type.get_canonical().kind == TypeKind.ULONG
50
51            assert fields[4].spelling == 'e'
52            assert not fields[4].type.is_const_qualified()
53            assert fields[4].type.kind == TypeKind.LONG
54            assert fields[4].type.get_canonical().kind == TypeKind.LONG
55
56            assert fields[5].spelling == 'f'
57            assert fields[5].type.is_const_qualified()
58            assert fields[5].type.kind == TypeKind.INT
59            assert fields[5].type.get_canonical().kind == TypeKind.INT
60
61            assert fields[6].spelling == 'g'
62            assert not fields[6].type.is_const_qualified()
63            assert fields[6].type.kind == TypeKind.POINTER
64            assert fields[6].type.get_pointee().kind == TypeKind.INT
65
66            assert fields[7].spelling == 'h'
67            assert not fields[7].type.is_const_qualified()
68            assert fields[7].type.kind == TypeKind.POINTER
69            assert fields[7].type.get_pointee().kind == TypeKind.POINTER
70            assert fields[7].type.get_pointee().get_pointee().kind == TypeKind.POINTER
71            assert fields[7].type.get_pointee().get_pointee().get_pointee().kind == TypeKind.INT
72
73            break
74
75    else:
76        assert False, "Didn't find teststruct??"
77
78
79constarrayInput="""
80struct teststruct {
81  void *A[2];
82};
83"""
84def testConstantArray():
85    index = Index.create()
86    tu = index.parse('t.c', unsaved_files = [('t.c',constarrayInput)])
87
88    for n in tu.cursor.get_children():
89        if n.spelling == 'teststruct':
90            fields = list(n.get_children())
91            assert fields[0].spelling == 'A'
92            assert fields[0].type.kind == TypeKind.CONSTANTARRAY
93            assert fields[0].type.get_array_element_type() is not None
94            assert fields[0].type.get_array_element_type().kind == TypeKind.POINTER
95            assert fields[0].type.get_array_size() == 2
96
97            break
98    else:
99        assert False, "Didn't find teststruct??"
100