• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import ctypes
2import gc
3
4from clang.cindex import CursorKind
5from clang.cindex import TemplateArgumentKind
6from clang.cindex import TranslationUnit
7from clang.cindex import TypeKind
8from .util import get_cursor
9from .util import get_cursors
10from .util import get_tu
11
12kInput = """\
13struct s0 {
14  int a;
15  int b;
16};
17
18struct s1;
19
20void f0(int a0, int a1) {
21  int l0, l1;
22
23  if (a0)
24    return;
25
26  for (;;) {
27    break;
28  }
29}
30"""
31
32def test_get_children():
33    tu = get_tu(kInput)
34
35    it = tu.cursor.get_children()
36    tu_nodes = list(it)
37
38    assert len(tu_nodes) == 3
39    for cursor in tu_nodes:
40        assert cursor.translation_unit is not None
41
42    assert tu_nodes[0] != tu_nodes[1]
43    assert tu_nodes[0].kind == CursorKind.STRUCT_DECL
44    assert tu_nodes[0].spelling == 's0'
45    assert tu_nodes[0].is_definition() == True
46    assert tu_nodes[0].location.file.name == 't.c'
47    assert tu_nodes[0].location.line == 1
48    assert tu_nodes[0].location.column == 8
49    assert tu_nodes[0].hash > 0
50    assert tu_nodes[0].translation_unit is not None
51
52    s0_nodes = list(tu_nodes[0].get_children())
53    assert len(s0_nodes) == 2
54    assert s0_nodes[0].kind == CursorKind.FIELD_DECL
55    assert s0_nodes[0].spelling == 'a'
56    assert s0_nodes[0].type.kind == TypeKind.INT
57    assert s0_nodes[1].kind == CursorKind.FIELD_DECL
58    assert s0_nodes[1].spelling == 'b'
59    assert s0_nodes[1].type.kind == TypeKind.INT
60
61    assert tu_nodes[1].kind == CursorKind.STRUCT_DECL
62    assert tu_nodes[1].spelling == 's1'
63    assert tu_nodes[1].displayname == 's1'
64    assert tu_nodes[1].is_definition() == False
65
66    assert tu_nodes[2].kind == CursorKind.FUNCTION_DECL
67    assert tu_nodes[2].spelling == 'f0'
68    assert tu_nodes[2].displayname == 'f0(int, int)'
69    assert tu_nodes[2].is_definition() == True
70
71def test_references():
72    """Ensure that references to TranslationUnit are kept."""
73    tu = get_tu('int x;')
74    cursors = list(tu.cursor.get_children())
75    assert len(cursors) > 0
76
77    cursor = cursors[0]
78    assert isinstance(cursor.translation_unit, TranslationUnit)
79
80    # Delete reference to TU and perform a full GC.
81    del tu
82    gc.collect()
83    assert isinstance(cursor.translation_unit, TranslationUnit)
84
85    # If the TU was destroyed, this should cause a segfault.
86    parent = cursor.semantic_parent
87
88def test_canonical():
89    source = 'struct X; struct X; struct X { int member; };'
90    tu = get_tu(source)
91
92    cursors = []
93    for cursor in tu.cursor.get_children():
94        if cursor.spelling == 'X':
95            cursors.append(cursor)
96
97    assert len(cursors) == 3
98    assert cursors[1].canonical == cursors[2].canonical
99
100def test_is_const_method():
101    """Ensure Cursor.is_const_method works."""
102    source = 'class X { void foo() const; void bar(); };'
103    tu = get_tu(source, lang='cpp')
104
105    cls = get_cursor(tu, 'X')
106    foo = get_cursor(tu, 'foo')
107    bar = get_cursor(tu, 'bar')
108    assert cls is not None
109    assert foo is not None
110    assert bar is not None
111
112    assert foo.is_const_method()
113    assert not bar.is_const_method()
114
115def test_is_converting_constructor():
116    """Ensure Cursor.is_converting_constructor works."""
117    source = 'class X { explicit X(int); X(double); X(); };'
118    tu = get_tu(source, lang='cpp')
119
120    xs = get_cursors(tu, 'X')
121
122    assert len(xs) == 4
123    assert xs[0].kind == CursorKind.CLASS_DECL
124    cs = xs[1:]
125    assert cs[0].kind == CursorKind.CONSTRUCTOR
126    assert cs[1].kind == CursorKind.CONSTRUCTOR
127    assert cs[2].kind == CursorKind.CONSTRUCTOR
128
129    assert not cs[0].is_converting_constructor()
130    assert cs[1].is_converting_constructor()
131    assert not cs[2].is_converting_constructor()
132
133
134def test_is_copy_constructor():
135    """Ensure Cursor.is_copy_constructor works."""
136    source = 'class X { X(); X(const X&); X(X&&); };'
137    tu = get_tu(source, lang='cpp')
138
139    xs = get_cursors(tu, 'X')
140    assert xs[0].kind == CursorKind.CLASS_DECL
141    cs = xs[1:]
142    assert cs[0].kind == CursorKind.CONSTRUCTOR
143    assert cs[1].kind == CursorKind.CONSTRUCTOR
144    assert cs[2].kind == CursorKind.CONSTRUCTOR
145
146    assert not cs[0].is_copy_constructor()
147    assert cs[1].is_copy_constructor()
148    assert not cs[2].is_copy_constructor()
149
150def test_is_default_constructor():
151    """Ensure Cursor.is_default_constructor works."""
152    source = 'class X { X(); X(int); };'
153    tu = get_tu(source, lang='cpp')
154
155    xs = get_cursors(tu, 'X')
156    assert xs[0].kind == CursorKind.CLASS_DECL
157    cs = xs[1:]
158    assert cs[0].kind == CursorKind.CONSTRUCTOR
159    assert cs[1].kind == CursorKind.CONSTRUCTOR
160
161    assert cs[0].is_default_constructor()
162    assert not cs[1].is_default_constructor()
163
164def test_is_move_constructor():
165    """Ensure Cursor.is_move_constructor works."""
166    source = 'class X { X(); X(const X&); X(X&&); };'
167    tu = get_tu(source, lang='cpp')
168
169    xs = get_cursors(tu, 'X')
170    assert xs[0].kind == CursorKind.CLASS_DECL
171    cs = xs[1:]
172    assert cs[0].kind == CursorKind.CONSTRUCTOR
173    assert cs[1].kind == CursorKind.CONSTRUCTOR
174    assert cs[2].kind == CursorKind.CONSTRUCTOR
175
176    assert not cs[0].is_move_constructor()
177    assert not cs[1].is_move_constructor()
178    assert cs[2].is_move_constructor()
179
180def test_is_default_method():
181    """Ensure Cursor.is_default_method works."""
182    source = 'class X { X() = default; }; class Y { Y(); };'
183    tu = get_tu(source, lang='cpp')
184
185    xs = get_cursors(tu, 'X')
186    ys = get_cursors(tu, 'Y')
187
188    assert len(xs) == 2
189    assert len(ys) == 2
190
191    xc = xs[1]
192    yc = ys[1]
193
194    assert xc.is_default_method()
195    assert not yc.is_default_method()
196
197def test_is_mutable_field():
198    """Ensure Cursor.is_mutable_field works."""
199    source = 'class X { int x_; mutable int y_; };'
200    tu = get_tu(source, lang='cpp')
201
202    cls = get_cursor(tu, 'X')
203    x_ = get_cursor(tu, 'x_')
204    y_ = get_cursor(tu, 'y_')
205    assert cls is not None
206    assert x_ is not None
207    assert y_ is not None
208
209    assert not x_.is_mutable_field()
210    assert y_.is_mutable_field()
211
212def test_is_static_method():
213    """Ensure Cursor.is_static_method works."""
214
215    source = 'class X { static void foo(); void bar(); };'
216    tu = get_tu(source, lang='cpp')
217
218    cls = get_cursor(tu, 'X')
219    foo = get_cursor(tu, 'foo')
220    bar = get_cursor(tu, 'bar')
221    assert cls is not None
222    assert foo is not None
223    assert bar is not None
224
225    assert foo.is_static_method()
226    assert not bar.is_static_method()
227
228def test_is_pure_virtual_method():
229    """Ensure Cursor.is_pure_virtual_method works."""
230    source = 'class X { virtual void foo() = 0; virtual void bar(); };'
231    tu = get_tu(source, lang='cpp')
232
233    cls = get_cursor(tu, 'X')
234    foo = get_cursor(tu, 'foo')
235    bar = get_cursor(tu, 'bar')
236    assert cls is not None
237    assert foo is not None
238    assert bar is not None
239
240    assert foo.is_pure_virtual_method()
241    assert not bar.is_pure_virtual_method()
242
243def test_is_virtual_method():
244    """Ensure Cursor.is_virtual_method works."""
245    source = 'class X { virtual void foo(); void bar(); };'
246    tu = get_tu(source, lang='cpp')
247
248    cls = get_cursor(tu, 'X')
249    foo = get_cursor(tu, 'foo')
250    bar = get_cursor(tu, 'bar')
251    assert cls is not None
252    assert foo is not None
253    assert bar is not None
254
255    assert foo.is_virtual_method()
256    assert not bar.is_virtual_method()
257
258def test_underlying_type():
259    tu = get_tu('typedef int foo;')
260    typedef = get_cursor(tu, 'foo')
261    assert typedef is not None
262
263    assert typedef.kind.is_declaration()
264    underlying = typedef.underlying_typedef_type
265    assert underlying.kind == TypeKind.INT
266
267kParentTest = """\
268        class C {
269            void f();
270        }
271
272        void C::f() { }
273    """
274def test_semantic_parent():
275    tu = get_tu(kParentTest, 'cpp')
276    curs = get_cursors(tu, 'f')
277    decl = get_cursor(tu, 'C')
278    assert(len(curs) == 2)
279    assert(curs[0].semantic_parent == curs[1].semantic_parent)
280    assert(curs[0].semantic_parent == decl)
281
282def test_lexical_parent():
283    tu = get_tu(kParentTest, 'cpp')
284    curs = get_cursors(tu, 'f')
285    decl = get_cursor(tu, 'C')
286    assert(len(curs) == 2)
287    assert(curs[0].lexical_parent != curs[1].lexical_parent)
288    assert(curs[0].lexical_parent == decl)
289    assert(curs[1].lexical_parent == tu.cursor)
290
291def test_enum_type():
292    tu = get_tu('enum TEST { FOO=1, BAR=2 };')
293    enum = get_cursor(tu, 'TEST')
294    assert enum is not None
295
296    assert enum.kind == CursorKind.ENUM_DECL
297    enum_type = enum.enum_type
298    assert enum_type.kind == TypeKind.UINT
299
300def test_enum_type_cpp():
301    tu = get_tu('enum TEST : long long { FOO=1, BAR=2 };', lang="cpp")
302    enum = get_cursor(tu, 'TEST')
303    assert enum is not None
304
305    assert enum.kind == CursorKind.ENUM_DECL
306    assert enum.enum_type.kind == TypeKind.LONGLONG
307
308def test_objc_type_encoding():
309    tu = get_tu('int i;', lang='objc')
310    i = get_cursor(tu, 'i')
311
312    assert i is not None
313    assert i.objc_type_encoding == 'i'
314
315def test_enum_values():
316    tu = get_tu('enum TEST { SPAM=1, EGG, HAM = EGG * 20};')
317    enum = get_cursor(tu, 'TEST')
318    assert enum is not None
319
320    assert enum.kind == CursorKind.ENUM_DECL
321
322    enum_constants = list(enum.get_children())
323    assert len(enum_constants) == 3
324
325    spam, egg, ham = enum_constants
326
327    assert spam.kind == CursorKind.ENUM_CONSTANT_DECL
328    assert spam.enum_value == 1
329    assert egg.kind == CursorKind.ENUM_CONSTANT_DECL
330    assert egg.enum_value == 2
331    assert ham.kind == CursorKind.ENUM_CONSTANT_DECL
332    assert ham.enum_value == 40
333
334def test_enum_values_cpp():
335    tu = get_tu('enum TEST : long long { SPAM = -1, HAM = 0x10000000000};', lang="cpp")
336    enum = get_cursor(tu, 'TEST')
337    assert enum is not None
338
339    assert enum.kind == CursorKind.ENUM_DECL
340
341    enum_constants = list(enum.get_children())
342    assert len(enum_constants) == 2
343
344    spam, ham = enum_constants
345
346    assert spam.kind == CursorKind.ENUM_CONSTANT_DECL
347    assert spam.enum_value == -1
348    assert ham.kind == CursorKind.ENUM_CONSTANT_DECL
349    assert ham.enum_value == 0x10000000000
350
351def test_annotation_attribute():
352    tu = get_tu('int foo (void) __attribute__ ((annotate("here be annotation attribute")));')
353
354    foo = get_cursor(tu, 'foo')
355    assert foo is not None
356
357    for c in foo.get_children():
358        if c.kind == CursorKind.ANNOTATE_ATTR:
359            assert c.displayname == "here be annotation attribute"
360            break
361    else:
362        assert False, "Couldn't find annotation"
363
364def test_result_type():
365    tu = get_tu('int foo();')
366    foo = get_cursor(tu, 'foo')
367
368    assert foo is not None
369    t = foo.result_type
370    assert t.kind == TypeKind.INT
371
372def test_get_tokens():
373    """Ensure we can map cursors back to tokens."""
374    tu = get_tu('int foo(int i);')
375    foo = get_cursor(tu, 'foo')
376
377    tokens = list(foo.get_tokens())
378    assert len(tokens) == 7
379    assert tokens[0].spelling == 'int'
380    assert tokens[1].spelling == 'foo'
381
382def test_get_arguments():
383    tu = get_tu('void foo(int i, int j);')
384    foo = get_cursor(tu, 'foo')
385    arguments = list(foo.get_arguments())
386
387    assert len(arguments) == 2
388    assert arguments[0].spelling == "i"
389    assert arguments[1].spelling == "j"
390
391kTemplateArgTest = """\
392        template <int kInt, typename T, bool kBool>
393        void foo();
394
395        template<>
396        void foo<-7, float, true>();
397    """
398
399def test_get_num_template_arguments():
400    tu = get_tu(kTemplateArgTest, lang='cpp')
401    foos = get_cursors(tu, 'foo')
402
403    assert foos[1].get_num_template_arguments() == 3
404
405def test_get_template_argument_kind():
406    tu = get_tu(kTemplateArgTest, lang='cpp')
407    foos = get_cursors(tu, 'foo')
408
409    assert foos[1].get_template_argument_kind(0) == TemplateArgumentKind.INTEGRAL
410    assert foos[1].get_template_argument_kind(1) == TemplateArgumentKind.TYPE
411    assert foos[1].get_template_argument_kind(2) == TemplateArgumentKind.INTEGRAL
412
413def test_get_template_argument_type():
414    tu = get_tu(kTemplateArgTest, lang='cpp')
415    foos = get_cursors(tu, 'foo')
416
417    assert foos[1].get_template_argument_type(1).kind == TypeKind.FLOAT
418
419def test_get_template_argument_value():
420    tu = get_tu(kTemplateArgTest, lang='cpp')
421    foos = get_cursors(tu, 'foo')
422
423    assert foos[1].get_template_argument_value(0) == -7
424    assert foos[1].get_template_argument_value(2) == True
425
426def test_get_template_argument_unsigned_value():
427    tu = get_tu(kTemplateArgTest, lang='cpp')
428    foos = get_cursors(tu, 'foo')
429
430    assert foos[1].get_template_argument_unsigned_value(0) == 2 ** 32 - 7
431    assert foos[1].get_template_argument_unsigned_value(2) == True
432
433def test_referenced():
434    tu = get_tu('void foo(); void bar() { foo(); }')
435    foo = get_cursor(tu, 'foo')
436    bar = get_cursor(tu, 'bar')
437    for c in bar.get_children():
438        if c.kind == CursorKind.CALL_EXPR:
439            assert c.referenced.spelling == foo.spelling
440            break
441
442def test_mangled_name():
443    kInputForMangling = """\
444    int foo(int, int);
445    """
446    tu = get_tu(kInputForMangling, lang='cpp')
447    foo = get_cursor(tu, 'foo')
448
449    # Since libclang does not link in targets, we cannot pass a triple to it
450    # and force the target. To enable this test to pass on all platforms, accept
451    # all valid manglings.
452    # [c-index-test handles this by running the source through clang, emitting
453    #  an AST file and running libclang on that AST file]
454    assert foo.mangled_name in ('_Z3fooii', '__Z3fooii', '?foo@@YAHHH')
455