blob: 9b5a16e785368ac87372a4158ad7a9bb3f99cbe0 [file] [log] [blame]
Gregory Szorc86057602012-02-17 07:44:46 +00001from clang.cindex import CursorKind
Gregory Szorc86057602012-02-17 07:44:46 +00002from clang.cindex import TypeKind
Gregory Szorc86057602012-02-17 07:44:46 +00003from nose.tools import raises
Gregory Szorc1f1988f2012-03-10 22:19:05 +00004from .util import get_cursor
5from .util import get_tu
Argyrios Kyrtzidis2312f5f2011-08-17 17:01:10 +00006
7kInput = """\
8
9typedef int I;
10
11struct teststruct {
12 int a;
13 I b;
14 long c;
15 unsigned long d;
16 signed long e;
17 const int f;
18 int *g;
19 int ***h;
20};
21
22"""
23
Gregory Szorc31cc38c2012-02-19 18:28:33 +000024def test_a_struct():
25 tu = get_tu(kInput)
Argyrios Kyrtzidis2312f5f2011-08-17 17:01:10 +000026
Gregory Szorc826fce52012-02-20 17:45:30 +000027 teststruct = get_cursor(tu, 'teststruct')
28 assert teststruct is not None, "Could not find teststruct."
29 fields = list(teststruct.get_children())
30 assert all(x.kind == CursorKind.FIELD_DECL for x in fields)
Argyrios Kyrtzidis2312f5f2011-08-17 17:01:10 +000031
Gregory Szorc826fce52012-02-20 17:45:30 +000032 assert fields[0].spelling == 'a'
33 assert not fields[0].type.is_const_qualified()
34 assert fields[0].type.kind == TypeKind.INT
35 assert fields[0].type.get_canonical().kind == TypeKind.INT
Argyrios Kyrtzidis2312f5f2011-08-17 17:01:10 +000036
Gregory Szorc826fce52012-02-20 17:45:30 +000037 assert fields[1].spelling == 'b'
38 assert not fields[1].type.is_const_qualified()
39 assert fields[1].type.kind == TypeKind.TYPEDEF
40 assert fields[1].type.get_canonical().kind == TypeKind.INT
41 assert fields[1].type.get_declaration().spelling == 'I'
Argyrios Kyrtzidis2312f5f2011-08-17 17:01:10 +000042
Gregory Szorc826fce52012-02-20 17:45:30 +000043 assert fields[2].spelling == 'c'
44 assert not fields[2].type.is_const_qualified()
45 assert fields[2].type.kind == TypeKind.LONG
46 assert fields[2].type.get_canonical().kind == TypeKind.LONG
Argyrios Kyrtzidis2312f5f2011-08-17 17:01:10 +000047
Gregory Szorc826fce52012-02-20 17:45:30 +000048 assert fields[3].spelling == 'd'
49 assert not fields[3].type.is_const_qualified()
50 assert fields[3].type.kind == TypeKind.ULONG
51 assert fields[3].type.get_canonical().kind == TypeKind.ULONG
Argyrios Kyrtzidis2312f5f2011-08-17 17:01:10 +000052
Gregory Szorc826fce52012-02-20 17:45:30 +000053 assert fields[4].spelling == 'e'
54 assert not fields[4].type.is_const_qualified()
55 assert fields[4].type.kind == TypeKind.LONG
56 assert fields[4].type.get_canonical().kind == TypeKind.LONG
Argyrios Kyrtzidis2312f5f2011-08-17 17:01:10 +000057
Gregory Szorc826fce52012-02-20 17:45:30 +000058 assert fields[5].spelling == 'f'
59 assert fields[5].type.is_const_qualified()
60 assert fields[5].type.kind == TypeKind.INT
61 assert fields[5].type.get_canonical().kind == TypeKind.INT
Argyrios Kyrtzidis2312f5f2011-08-17 17:01:10 +000062
Gregory Szorc826fce52012-02-20 17:45:30 +000063 assert fields[6].spelling == 'g'
64 assert not fields[6].type.is_const_qualified()
65 assert fields[6].type.kind == TypeKind.POINTER
66 assert fields[6].type.get_pointee().kind == TypeKind.INT
Argyrios Kyrtzidis2312f5f2011-08-17 17:01:10 +000067
Gregory Szorc826fce52012-02-20 17:45:30 +000068 assert fields[7].spelling == 'h'
69 assert not fields[7].type.is_const_qualified()
70 assert fields[7].type.kind == TypeKind.POINTER
71 assert fields[7].type.get_pointee().kind == TypeKind.POINTER
72 assert fields[7].type.get_pointee().get_pointee().kind == TypeKind.POINTER
73 assert fields[7].type.get_pointee().get_pointee().get_pointee().kind == TypeKind.INT
Douglas Gregor38d2d552011-10-19 05:50:34 +000074
75constarrayInput="""
76struct teststruct {
77 void *A[2];
78};
79"""
80def testConstantArray():
Gregory Szorc31cc38c2012-02-19 18:28:33 +000081 tu = get_tu(constarrayInput)
Douglas Gregor38d2d552011-10-19 05:50:34 +000082
Gregory Szorc826fce52012-02-20 17:45:30 +000083 teststruct = get_cursor(tu, 'teststruct')
84 assert teststruct is not None, "Didn't find teststruct??"
85 fields = list(teststruct.get_children())
86 assert fields[0].spelling == 'A'
87 assert fields[0].type.kind == TypeKind.CONSTANTARRAY
88 assert fields[0].type.get_array_element_type() is not None
89 assert fields[0].type.get_array_element_type().kind == TypeKind.POINTER
90 assert fields[0].type.get_array_size() == 2
Gregory Szorc96ad6332012-02-05 19:42:06 +000091
Gregory Szorc7eb691a2012-02-20 17:44:49 +000092def test_equal():
93 """Ensure equivalence operators work on Type."""
94 source = 'int a; int b; void *v;'
95 tu = get_tu(source)
96
Gregory Szorc826fce52012-02-20 17:45:30 +000097 a = get_cursor(tu, 'a')
98 b = get_cursor(tu, 'b')
99 v = get_cursor(tu, 'v')
Gregory Szorc7eb691a2012-02-20 17:44:49 +0000100
101 assert a is not None
102 assert b is not None
103 assert v is not None
104
105 assert a.type == b.type
106 assert a.type != v.type
107
108 assert a.type != None
109 assert a.type != 'foo'
110
Gregory Szorc6e67eed2012-04-15 18:51:10 +0000111def test_typekind_spelling():
112 """Ensure TypeKind.spelling works."""
113 tu = get_tu('int a;')
114 a = get_cursor(tu, 'a')
115
116 assert a is not None
117 assert a.type.kind.spelling == 'Int'
118
Gregory Szorc826fce52012-02-20 17:45:30 +0000119def test_function_argument_types():
120 """Ensure that Type.argument_types() works as expected."""
121 tu = get_tu('void f(int, int);')
122 f = get_cursor(tu, 'f')
123 assert f is not None
124
125 args = f.type.argument_types()
126 assert args is not None
127 assert len(args) == 2
128
129 t0 = args[0]
130 assert t0 is not None
131 assert t0.kind == TypeKind.INT
132
133 t1 = args[1]
134 assert t1 is not None
135 assert t1.kind == TypeKind.INT
136
137 args2 = list(args)
138 assert len(args2) == 2
139 assert t0 == args2[0]
140 assert t1 == args2[1]
141
142@raises(TypeError)
143def test_argument_types_string_key():
144 """Ensure that non-int keys raise a TypeError."""
145 tu = get_tu('void f(int, int);')
146 f = get_cursor(tu, 'f')
147 assert f is not None
148
149 args = f.type.argument_types()
150 assert len(args) == 2
151
152 args['foo']
153
154@raises(IndexError)
155def test_argument_types_negative_index():
156 """Ensure that negative indexes on argument_types Raises an IndexError."""
157 tu = get_tu('void f(int, int);')
158 f = get_cursor(tu, 'f')
159 args = f.type.argument_types()
160
161 args[-1]
162
163@raises(IndexError)
164def test_argument_types_overflow_index():
165 """Ensure that indexes beyond the length of Type.argument_types() raise."""
166 tu = get_tu('void f(int, int);')
167 f = get_cursor(tu, 'f')
168 args = f.type.argument_types()
169
170 args[2]
171
172@raises(Exception)
173def test_argument_types_invalid_type():
174 """Ensure that obtaining argument_types on a Type without them raises."""
175 tu = get_tu('int i;')
176 i = get_cursor(tu, 'i')
177 assert i is not None
178
179 i.type.argument_types()
180
Gregory Szorc96ad6332012-02-05 19:42:06 +0000181def test_is_pod():
Gregory Szorc82613452012-02-20 17:58:40 +0000182 """Ensure Type.is_pod() works."""
Gregory Szorc31cc38c2012-02-19 18:28:33 +0000183 tu = get_tu('int i; void f();')
Gregory Szorc826fce52012-02-20 17:45:30 +0000184 i = get_cursor(tu, 'i')
185 f = get_cursor(tu, 'f')
Gregory Szorc96ad6332012-02-05 19:42:06 +0000186
187 assert i is not None
188 assert f is not None
189
190 assert i.type.is_pod()
191 assert not f.type.is_pod()
Gregory Szorc86057602012-02-17 07:44:46 +0000192
Gregory Szorc31cc38c2012-02-19 18:28:33 +0000193def test_function_variadic():
194 """Ensure Type.is_function_variadic works."""
Gregory Szorc86057602012-02-17 07:44:46 +0000195
Gregory Szorc31cc38c2012-02-19 18:28:33 +0000196 source ="""
197#include <stdarg.h>
198
199void foo(int a, ...);
200void bar(int a, int b);
201"""
202
203 tu = get_tu(source)
Gregory Szorc826fce52012-02-20 17:45:30 +0000204 foo = get_cursor(tu, 'foo')
205 bar = get_cursor(tu, 'bar')
Gregory Szorc31cc38c2012-02-19 18:28:33 +0000206
207 assert foo is not None
208 assert bar is not None
209
210 assert isinstance(foo.type.is_function_variadic(), bool)
211 assert foo.type.is_function_variadic()
212 assert not bar.type.is_function_variadic()
213
214def test_element_type():
Gregory Szorc82613452012-02-20 17:58:40 +0000215 """Ensure Type.element_type works."""
Gregory Szorc31cc38c2012-02-19 18:28:33 +0000216 tu = get_tu('int i[5];')
Gregory Szorc826fce52012-02-20 17:45:30 +0000217 i = get_cursor(tu, 'i')
Gregory Szorc31cc38c2012-02-19 18:28:33 +0000218 assert i is not None
219
Gregory Szorc86057602012-02-17 07:44:46 +0000220 assert i.type.kind == TypeKind.CONSTANTARRAY
221 assert i.type.element_type.kind == TypeKind.INT
222
223@raises(Exception)
224def test_invalid_element_type():
225 """Ensure Type.element_type raises if type doesn't have elements."""
Gregory Szorc31cc38c2012-02-19 18:28:33 +0000226 tu = get_tu('int i;')
Gregory Szorc826fce52012-02-20 17:45:30 +0000227 i = get_cursor(tu, 'i')
Gregory Szorc31cc38c2012-02-19 18:28:33 +0000228 assert i is not None
Gregory Szorc86057602012-02-17 07:44:46 +0000229 i.element_type
Gregory Szorcbf8ca002012-02-17 07:47:38 +0000230
231def test_element_count():
Gregory Szorc82613452012-02-20 17:58:40 +0000232 """Ensure Type.element_count works."""
Gregory Szorc31cc38c2012-02-19 18:28:33 +0000233 tu = get_tu('int i[5]; int j;')
Gregory Szorc826fce52012-02-20 17:45:30 +0000234 i = get_cursor(tu, 'i')
235 j = get_cursor(tu, 'j')
Gregory Szorcbf8ca002012-02-17 07:47:38 +0000236
237 assert i is not None
238 assert j is not None
239
240 assert i.type.element_count == 5
241
242 try:
243 j.type.element_count
244 assert False
245 except:
246 assert True
Gregory Szorc0e1f4f82012-02-20 17:58:02 +0000247
248def test_is_volatile_qualified():
249 """Ensure Type.is_volatile_qualified works."""
250
251 tu = get_tu('volatile int i = 4; int j = 2;')
252
253 i = get_cursor(tu, 'i')
254 j = get_cursor(tu, 'j')
255
256 assert i is not None
257 assert j is not None
258
259 assert isinstance(i.type.is_volatile_qualified(), bool)
260 assert i.type.is_volatile_qualified()
261 assert not j.type.is_volatile_qualified()
262
263def test_is_restrict_qualified():
264 """Ensure Type.is_restrict_qualified works."""
265
Anders Waldenborge1f61c02012-05-02 19:35:37 +0000266 tu = get_tu('struct s { void * restrict i; void * j; };')
Gregory Szorc0e1f4f82012-02-20 17:58:02 +0000267
268 i = get_cursor(tu, 'i')
269 j = get_cursor(tu, 'j')
270
271 assert i is not None
272 assert j is not None
273
274 assert isinstance(i.type.is_restrict_qualified(), bool)
275 assert i.type.is_restrict_qualified()
276 assert not j.type.is_restrict_qualified()