Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / bindings / python / tests / cindex / test_cursor.py
blob84cd8139418447a3c2efa87d6d521d106a063c9e
1 import os
2 from clang.cindex import Config
4 if "CLANG_LIBRARY_PATH" in os.environ:
5 Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
7 import ctypes
8 import gc
9 import unittest
11 from clang.cindex import AvailabilityKind
12 from clang.cindex import CursorKind
13 from clang.cindex import TemplateArgumentKind
14 from clang.cindex import TranslationUnit
15 from clang.cindex import TypeKind
16 from .util import get_cursor
17 from .util import get_cursors
18 from .util import get_tu
21 kInput = """\
22 struct s0 {
23 int a;
24 int b;
27 struct s1;
29 void f0(int a0, int a1) {
30 int l0, l1;
32 if (a0)
33 return;
35 for (;;) {
36 break;
39 """
41 kParentTest = """\
42 class C {
43 void f();
46 void C::f() { }
47 """
49 kTemplateArgTest = """\
50 template <int kInt, typename T, bool kBool>
51 void foo();
53 template<>
54 void foo<-7, float, true>();
55 """
58 class TestCursor(unittest.TestCase):
59 def test_get_children(self):
60 tu = get_tu(kInput)
62 it = tu.cursor.get_children()
63 tu_nodes = list(it)
65 self.assertEqual(len(tu_nodes), 3)
66 for cursor in tu_nodes:
67 self.assertIsNotNone(cursor.translation_unit)
69 self.assertNotEqual(tu_nodes[0], tu_nodes[1])
70 self.assertEqual(tu_nodes[0].kind, CursorKind.STRUCT_DECL)
71 self.assertEqual(tu_nodes[0].spelling, "s0")
72 self.assertEqual(tu_nodes[0].is_definition(), True)
73 self.assertEqual(tu_nodes[0].location.file.name, "t.c")
74 self.assertEqual(tu_nodes[0].location.line, 1)
75 self.assertEqual(tu_nodes[0].location.column, 8)
76 self.assertGreater(tu_nodes[0].hash, 0)
77 self.assertIsNotNone(tu_nodes[0].translation_unit)
79 s0_nodes = list(tu_nodes[0].get_children())
80 self.assertEqual(len(s0_nodes), 2)
81 self.assertEqual(s0_nodes[0].kind, CursorKind.FIELD_DECL)
82 self.assertEqual(s0_nodes[0].spelling, "a")
83 self.assertEqual(s0_nodes[0].type.kind, TypeKind.INT)
84 self.assertEqual(s0_nodes[1].kind, CursorKind.FIELD_DECL)
85 self.assertEqual(s0_nodes[1].spelling, "b")
86 self.assertEqual(s0_nodes[1].type.kind, TypeKind.INT)
88 self.assertEqual(tu_nodes[1].kind, CursorKind.STRUCT_DECL)
89 self.assertEqual(tu_nodes[1].spelling, "s1")
90 self.assertEqual(tu_nodes[1].displayname, "s1")
91 self.assertEqual(tu_nodes[1].is_definition(), False)
93 self.assertEqual(tu_nodes[2].kind, CursorKind.FUNCTION_DECL)
94 self.assertEqual(tu_nodes[2].spelling, "f0")
95 self.assertEqual(tu_nodes[2].displayname, "f0(int, int)")
96 self.assertEqual(tu_nodes[2].is_definition(), True)
98 def test_references(self):
99 """Ensure that references to TranslationUnit are kept."""
100 tu = get_tu("int x;")
101 cursors = list(tu.cursor.get_children())
102 self.assertGreater(len(cursors), 0)
104 cursor = cursors[0]
105 self.assertIsInstance(cursor.translation_unit, TranslationUnit)
107 # Delete reference to TU and perform a full GC.
108 del tu
109 gc.collect()
110 self.assertIsInstance(cursor.translation_unit, TranslationUnit)
112 # If the TU was destroyed, this should cause a segfault.
113 parent = cursor.semantic_parent
115 def test_canonical(self):
116 source = "struct X; struct X; struct X { int member; };"
117 tu = get_tu(source)
119 cursors = []
120 for cursor in tu.cursor.get_children():
121 if cursor.spelling == "X":
122 cursors.append(cursor)
124 self.assertEqual(len(cursors), 3)
125 self.assertEqual(cursors[1].canonical, cursors[2].canonical)
127 def test_is_const_method(self):
128 """Ensure Cursor.is_const_method works."""
129 source = "class X { void foo() const; void bar(); };"
130 tu = get_tu(source, lang="cpp")
132 cls = get_cursor(tu, "X")
133 foo = get_cursor(tu, "foo")
134 bar = get_cursor(tu, "bar")
135 self.assertIsNotNone(cls)
136 self.assertIsNotNone(foo)
137 self.assertIsNotNone(bar)
139 self.assertTrue(foo.is_const_method())
140 self.assertFalse(bar.is_const_method())
142 def test_is_converting_constructor(self):
143 """Ensure Cursor.is_converting_constructor works."""
144 source = "class X { explicit X(int); X(double); X(); };"
145 tu = get_tu(source, lang="cpp")
147 xs = get_cursors(tu, "X")
149 self.assertEqual(len(xs), 4)
150 self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL)
151 cs = xs[1:]
152 self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR)
153 self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR)
154 self.assertEqual(cs[2].kind, CursorKind.CONSTRUCTOR)
156 self.assertFalse(cs[0].is_converting_constructor())
157 self.assertTrue(cs[1].is_converting_constructor())
158 self.assertFalse(cs[2].is_converting_constructor())
160 def test_is_copy_constructor(self):
161 """Ensure Cursor.is_copy_constructor works."""
162 source = "class X { X(); X(const X&); X(X&&); };"
163 tu = get_tu(source, lang="cpp")
165 xs = get_cursors(tu, "X")
166 self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL)
167 cs = xs[1:]
168 self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR)
169 self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR)
170 self.assertEqual(cs[2].kind, CursorKind.CONSTRUCTOR)
172 self.assertFalse(cs[0].is_copy_constructor())
173 self.assertTrue(cs[1].is_copy_constructor())
174 self.assertFalse(cs[2].is_copy_constructor())
176 def test_is_default_constructor(self):
177 """Ensure Cursor.is_default_constructor works."""
178 source = "class X { X(); X(int); };"
179 tu = get_tu(source, lang="cpp")
181 xs = get_cursors(tu, "X")
182 self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL)
183 cs = xs[1:]
184 self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR)
185 self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR)
187 self.assertTrue(cs[0].is_default_constructor())
188 self.assertFalse(cs[1].is_default_constructor())
190 def test_is_move_constructor(self):
191 """Ensure Cursor.is_move_constructor works."""
192 source = "class X { X(); X(const X&); X(X&&); };"
193 tu = get_tu(source, lang="cpp")
195 xs = get_cursors(tu, "X")
196 self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL)
197 cs = xs[1:]
198 self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR)
199 self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR)
200 self.assertEqual(cs[2].kind, CursorKind.CONSTRUCTOR)
202 self.assertFalse(cs[0].is_move_constructor())
203 self.assertFalse(cs[1].is_move_constructor())
204 self.assertTrue(cs[2].is_move_constructor())
206 def test_is_default_method(self):
207 """Ensure Cursor.is_default_method works."""
208 source = "class X { X() = default; }; class Y { Y(); };"
209 tu = get_tu(source, lang="cpp")
211 xs = get_cursors(tu, "X")
212 ys = get_cursors(tu, "Y")
214 self.assertEqual(len(xs), 2)
215 self.assertEqual(len(ys), 2)
217 xc = xs[1]
218 yc = ys[1]
220 self.assertTrue(xc.is_default_method())
221 self.assertFalse(yc.is_default_method())
223 def test_is_move_assignment_operator_method(self):
224 """Ensure Cursor.is_move_assignment_operator_method works."""
225 source_with_move_assignment_operators = """
226 struct Foo {
227 // Those are move-assignment operators
228 bool operator=(const Foo&&);
229 bool operator=(Foo&&);
230 bool operator=(volatile Foo&&);
231 bool operator=(const volatile Foo&&);
233 // Positive-check that the recognition works for templated classes too
234 template <typename T>
235 class Bar {
236 bool operator=(const Bar&&);
237 bool operator=(Bar<T>&&);
238 bool operator=(volatile Bar&&);
239 bool operator=(const volatile Bar<T>&&);
242 source_without_move_assignment_operators = """
243 struct Foo {
244 // Those are not move-assignment operators
245 template<typename T>
246 bool operator=(const T&&);
247 bool operator=(const bool&&);
248 bool operator=(char&&);
249 bool operator=(volatile unsigned int&&);
250 bool operator=(const volatile unsigned char&&);
251 bool operator=(int);
252 bool operator=(Foo);
255 tu_with_move_assignment_operators = get_tu(
256 source_with_move_assignment_operators, lang="cpp"
258 tu_without_move_assignment_operators = get_tu(
259 source_without_move_assignment_operators, lang="cpp"
262 move_assignment_operators_cursors = get_cursors(
263 tu_with_move_assignment_operators, "operator="
265 non_move_assignment_operators_cursors = get_cursors(
266 tu_without_move_assignment_operators, "operator="
269 self.assertEqual(len(move_assignment_operators_cursors), 8)
270 self.assertTrue(len(non_move_assignment_operators_cursors), 7)
272 self.assertTrue(
273 all(
275 cursor.is_move_assignment_operator_method()
276 for cursor in move_assignment_operators_cursors
280 self.assertFalse(
281 any(
283 cursor.is_move_assignment_operator_method()
284 for cursor in non_move_assignment_operators_cursors
289 def test_is_explicit_method(self):
290 """Ensure Cursor.is_explicit_method works."""
291 source_with_explicit_methods = """
292 struct Foo {
293 // Those are explicit
294 explicit Foo(double);
295 explicit(true) Foo(char);
296 explicit operator double();
297 explicit(true) operator char();
300 source_without_explicit_methods = """
301 struct Foo {
302 // Those are not explicit
303 Foo(int);
304 explicit(false) Foo(float);
305 operator int();
306 explicit(false) operator float();
309 tu_with_explicit_methods = get_tu(source_with_explicit_methods, lang="cpp")
310 tu_without_explicit_methods = get_tu(
311 source_without_explicit_methods, lang="cpp"
314 explicit_methods_cursors = [
315 *get_cursors(tu_with_explicit_methods, "Foo")[1:],
316 get_cursor(tu_with_explicit_methods, "operator double"),
317 get_cursor(tu_with_explicit_methods, "operator char"),
320 non_explicit_methods_cursors = [
321 *get_cursors(tu_without_explicit_methods, "Foo")[1:],
322 get_cursor(tu_without_explicit_methods, "operator int"),
323 get_cursor(tu_without_explicit_methods, "operator float"),
326 self.assertEqual(len(explicit_methods_cursors), 4)
327 self.assertTrue(len(non_explicit_methods_cursors), 4)
329 self.assertTrue(
330 all([cursor.is_explicit_method() for cursor in explicit_methods_cursors])
332 self.assertFalse(
333 any(
334 [cursor.is_explicit_method() for cursor in non_explicit_methods_cursors]
338 def test_is_mutable_field(self):
339 """Ensure Cursor.is_mutable_field works."""
340 source = "class X { int x_; mutable int y_; };"
341 tu = get_tu(source, lang="cpp")
343 cls = get_cursor(tu, "X")
344 x_ = get_cursor(tu, "x_")
345 y_ = get_cursor(tu, "y_")
346 self.assertIsNotNone(cls)
347 self.assertIsNotNone(x_)
348 self.assertIsNotNone(y_)
350 self.assertFalse(x_.is_mutable_field())
351 self.assertTrue(y_.is_mutable_field())
353 def test_is_static_method(self):
354 """Ensure Cursor.is_static_method works."""
356 source = "class X { static void foo(); void bar(); };"
357 tu = get_tu(source, lang="cpp")
359 cls = get_cursor(tu, "X")
360 foo = get_cursor(tu, "foo")
361 bar = get_cursor(tu, "bar")
362 self.assertIsNotNone(cls)
363 self.assertIsNotNone(foo)
364 self.assertIsNotNone(bar)
366 self.assertTrue(foo.is_static_method())
367 self.assertFalse(bar.is_static_method())
369 def test_is_pure_virtual_method(self):
370 """Ensure Cursor.is_pure_virtual_method works."""
371 source = "class X { virtual void foo() = 0; virtual void bar(); };"
372 tu = get_tu(source, lang="cpp")
374 cls = get_cursor(tu, "X")
375 foo = get_cursor(tu, "foo")
376 bar = get_cursor(tu, "bar")
377 self.assertIsNotNone(cls)
378 self.assertIsNotNone(foo)
379 self.assertIsNotNone(bar)
381 self.assertTrue(foo.is_pure_virtual_method())
382 self.assertFalse(bar.is_pure_virtual_method())
384 def test_is_virtual_method(self):
385 """Ensure Cursor.is_virtual_method works."""
386 source = "class X { virtual void foo(); void bar(); };"
387 tu = get_tu(source, lang="cpp")
389 cls = get_cursor(tu, "X")
390 foo = get_cursor(tu, "foo")
391 bar = get_cursor(tu, "bar")
392 self.assertIsNotNone(cls)
393 self.assertIsNotNone(foo)
394 self.assertIsNotNone(bar)
396 self.assertTrue(foo.is_virtual_method())
397 self.assertFalse(bar.is_virtual_method())
399 def test_is_abstract_record(self):
400 """Ensure Cursor.is_abstract_record works."""
401 source = "struct X { virtual void x() = 0; }; struct Y : X { void x(); };"
402 tu = get_tu(source, lang="cpp")
404 cls = get_cursor(tu, "X")
405 self.assertTrue(cls.is_abstract_record())
407 cls = get_cursor(tu, "Y")
408 self.assertFalse(cls.is_abstract_record())
410 def test_is_scoped_enum(self):
411 """Ensure Cursor.is_scoped_enum works."""
412 source = "class X {}; enum RegularEnum {}; enum class ScopedEnum {};"
413 tu = get_tu(source, lang="cpp")
415 cls = get_cursor(tu, "X")
416 regular_enum = get_cursor(tu, "RegularEnum")
417 scoped_enum = get_cursor(tu, "ScopedEnum")
418 self.assertIsNotNone(cls)
419 self.assertIsNotNone(regular_enum)
420 self.assertIsNotNone(scoped_enum)
422 self.assertFalse(cls.is_scoped_enum())
423 self.assertFalse(regular_enum.is_scoped_enum())
424 self.assertTrue(scoped_enum.is_scoped_enum())
426 def test_underlying_type(self):
427 tu = get_tu("typedef int foo;")
428 typedef = get_cursor(tu, "foo")
429 self.assertIsNotNone(typedef)
431 self.assertTrue(typedef.kind.is_declaration())
432 underlying = typedef.underlying_typedef_type
433 self.assertEqual(underlying.kind, TypeKind.INT)
435 def test_semantic_parent(self):
436 tu = get_tu(kParentTest, "cpp")
437 curs = get_cursors(tu, "f")
438 decl = get_cursor(tu, "C")
439 self.assertEqual(len(curs), 2)
440 self.assertEqual(curs[0].semantic_parent, curs[1].semantic_parent)
441 self.assertEqual(curs[0].semantic_parent, decl)
443 def test_lexical_parent(self):
444 tu = get_tu(kParentTest, "cpp")
445 curs = get_cursors(tu, "f")
446 decl = get_cursor(tu, "C")
447 self.assertEqual(len(curs), 2)
448 self.assertNotEqual(curs[0].lexical_parent, curs[1].lexical_parent)
449 self.assertEqual(curs[0].lexical_parent, decl)
450 self.assertEqual(curs[1].lexical_parent, tu.cursor)
452 def test_enum_type(self):
453 tu = get_tu("enum TEST { FOO=1, BAR=2 };")
454 enum = get_cursor(tu, "TEST")
455 self.assertIsNotNone(enum)
457 self.assertEqual(enum.kind, CursorKind.ENUM_DECL)
458 enum_type = enum.enum_type
459 self.assertIn(enum_type.kind, (TypeKind.UINT, TypeKind.INT))
461 def test_enum_type_cpp(self):
462 tu = get_tu("enum TEST : long long { FOO=1, BAR=2 };", lang="cpp")
463 enum = get_cursor(tu, "TEST")
464 self.assertIsNotNone(enum)
466 self.assertEqual(enum.kind, CursorKind.ENUM_DECL)
467 self.assertEqual(enum.enum_type.kind, TypeKind.LONGLONG)
469 def test_objc_type_encoding(self):
470 tu = get_tu("int i;", lang="objc")
471 i = get_cursor(tu, "i")
473 self.assertIsNotNone(i)
474 self.assertEqual(i.objc_type_encoding, "i")
476 def test_enum_values(self):
477 tu = get_tu("enum TEST { SPAM=1, EGG, HAM = EGG * 20};")
478 enum = get_cursor(tu, "TEST")
479 self.assertIsNotNone(enum)
481 self.assertEqual(enum.kind, CursorKind.ENUM_DECL)
483 enum_constants = list(enum.get_children())
484 self.assertEqual(len(enum_constants), 3)
486 spam, egg, ham = enum_constants
488 self.assertEqual(spam.kind, CursorKind.ENUM_CONSTANT_DECL)
489 self.assertEqual(spam.enum_value, 1)
490 self.assertEqual(egg.kind, CursorKind.ENUM_CONSTANT_DECL)
491 self.assertEqual(egg.enum_value, 2)
492 self.assertEqual(ham.kind, CursorKind.ENUM_CONSTANT_DECL)
493 self.assertEqual(ham.enum_value, 40)
495 def test_enum_values_cpp(self):
496 tu = get_tu(
497 "enum TEST : long long { SPAM = -1, HAM = 0x10000000000};", lang="cpp"
499 enum = get_cursor(tu, "TEST")
500 self.assertIsNotNone(enum)
502 self.assertEqual(enum.kind, CursorKind.ENUM_DECL)
504 enum_constants = list(enum.get_children())
505 self.assertEqual(len(enum_constants), 2)
507 spam, ham = enum_constants
509 self.assertEqual(spam.kind, CursorKind.ENUM_CONSTANT_DECL)
510 self.assertEqual(spam.enum_value, -1)
511 self.assertEqual(ham.kind, CursorKind.ENUM_CONSTANT_DECL)
512 self.assertEqual(ham.enum_value, 0x10000000000)
514 def test_annotation_attribute(self):
515 tu = get_tu(
516 'int foo (void) __attribute__ ((annotate("here be annotation attribute")));'
519 foo = get_cursor(tu, "foo")
520 self.assertIsNotNone(foo)
522 for c in foo.get_children():
523 if c.kind == CursorKind.ANNOTATE_ATTR:
524 self.assertEqual(c.displayname, "here be annotation attribute")
525 break
526 else:
527 self.fail("Couldn't find annotation")
529 def test_annotation_template(self):
530 annotation = '__attribute__ ((annotate("annotation")))'
531 for source, kind in [
532 ("int foo (T value) %s;", CursorKind.FUNCTION_TEMPLATE),
533 ("class %s foo {};", CursorKind.CLASS_TEMPLATE),
535 source = "template<typename T> " + (source % annotation)
536 tu = get_tu(source, lang="cpp")
538 foo = get_cursor(tu, "foo")
539 self.assertIsNotNone(foo)
540 self.assertEqual(foo.kind, kind)
542 for c in foo.get_children():
543 if c.kind == CursorKind.ANNOTATE_ATTR:
544 self.assertEqual(c.displayname, "annotation")
545 break
546 else:
547 self.fail("Couldn't find annotation for {}".format(kind))
549 def test_result_type(self):
550 tu = get_tu("int foo();")
551 foo = get_cursor(tu, "foo")
553 self.assertIsNotNone(foo)
554 t = foo.result_type
555 self.assertEqual(t.kind, TypeKind.INT)
557 def test_result_type_objc_method_decl(self):
558 code = """\
559 @interface Interface : NSObject
560 -(void)voidMethod;
561 @end
563 tu = get_tu(code, lang="objc")
564 cursor = get_cursor(tu, "voidMethod")
565 result_type = cursor.result_type
566 self.assertEqual(cursor.kind, CursorKind.OBJC_INSTANCE_METHOD_DECL)
567 self.assertEqual(result_type.kind, TypeKind.VOID)
569 def test_availability(self):
570 tu = get_tu("class A { A(A const&) = delete; };", lang="cpp")
572 # AvailabilityKind.AVAILABLE
573 cursor = get_cursor(tu, "A")
574 self.assertEqual(cursor.kind, CursorKind.CLASS_DECL)
575 self.assertEqual(cursor.availability, AvailabilityKind.AVAILABLE)
577 # AvailabilityKind.NOT_AVAILABLE
578 cursors = get_cursors(tu, "A")
579 for c in cursors:
580 if c.kind == CursorKind.CONSTRUCTOR:
581 self.assertEqual(c.availability, AvailabilityKind.NOT_AVAILABLE)
582 break
583 else:
584 self.fail("Could not find cursor for deleted constructor")
586 # AvailabilityKind.DEPRECATED
587 tu = get_tu("void test() __attribute__((deprecated));", lang="cpp")
588 cursor = get_cursor(tu, "test")
589 self.assertEqual(cursor.availability, AvailabilityKind.DEPRECATED)
591 # AvailabilityKind.NOT_ACCESSIBLE is only used in the code completion results
593 def test_get_tokens(self):
594 """Ensure we can map cursors back to tokens."""
595 tu = get_tu("int foo(int i);")
596 foo = get_cursor(tu, "foo")
598 tokens = list(foo.get_tokens())
599 self.assertEqual(len(tokens), 6)
600 self.assertEqual(tokens[0].spelling, "int")
601 self.assertEqual(tokens[1].spelling, "foo")
603 def test_get_token_cursor(self):
604 """Ensure we can map tokens to cursors."""
605 tu = get_tu("class A {}; int foo(A var = A());", lang="cpp")
606 foo = get_cursor(tu, "foo")
608 for cursor in foo.walk_preorder():
609 if cursor.kind.is_expression() and not cursor.kind.is_statement():
610 break
611 else:
612 self.fail("Could not find default value expression")
614 tokens = list(cursor.get_tokens())
615 self.assertEqual(len(tokens), 4, [t.spelling for t in tokens])
616 self.assertEqual(tokens[0].spelling, "=")
617 self.assertEqual(tokens[1].spelling, "A")
618 self.assertEqual(tokens[2].spelling, "(")
619 self.assertEqual(tokens[3].spelling, ")")
620 t_cursor = tokens[1].cursor
621 self.assertEqual(t_cursor.kind, CursorKind.TYPE_REF)
622 r_cursor = t_cursor.referenced # should not raise an exception
623 self.assertEqual(r_cursor.kind, CursorKind.CLASS_DECL)
625 def test_get_arguments(self):
626 tu = get_tu("void foo(int i, int j);")
627 foo = get_cursor(tu, "foo")
628 arguments = list(foo.get_arguments())
630 self.assertEqual(len(arguments), 2)
631 self.assertEqual(arguments[0].spelling, "i")
632 self.assertEqual(arguments[1].spelling, "j")
634 def test_get_num_template_arguments(self):
635 tu = get_tu(kTemplateArgTest, lang="cpp")
636 foos = get_cursors(tu, "foo")
638 self.assertEqual(foos[1].get_num_template_arguments(), 3)
640 def test_get_template_argument_kind(self):
641 tu = get_tu(kTemplateArgTest, lang="cpp")
642 foos = get_cursors(tu, "foo")
644 self.assertEqual(
645 foos[1].get_template_argument_kind(0), TemplateArgumentKind.INTEGRAL
647 self.assertEqual(
648 foos[1].get_template_argument_kind(1), TemplateArgumentKind.TYPE
650 self.assertEqual(
651 foos[1].get_template_argument_kind(2), TemplateArgumentKind.INTEGRAL
654 def test_get_template_argument_type(self):
655 tu = get_tu(kTemplateArgTest, lang="cpp")
656 foos = get_cursors(tu, "foo")
658 self.assertEqual(foos[1].get_template_argument_type(1).kind, TypeKind.FLOAT)
660 def test_get_template_argument_value(self):
661 tu = get_tu(kTemplateArgTest, lang="cpp")
662 foos = get_cursors(tu, "foo")
664 self.assertEqual(foos[1].get_template_argument_value(0), -7)
665 self.assertEqual(foos[1].get_template_argument_value(2), True)
667 def test_get_template_argument_unsigned_value(self):
668 tu = get_tu(kTemplateArgTest, lang="cpp")
669 foos = get_cursors(tu, "foo")
671 self.assertEqual(foos[1].get_template_argument_unsigned_value(0), 2**32 - 7)
672 self.assertEqual(foos[1].get_template_argument_unsigned_value(2), True)
674 def test_referenced(self):
675 tu = get_tu("void foo(); void bar() { foo(); }")
676 foo = get_cursor(tu, "foo")
677 bar = get_cursor(tu, "bar")
678 for c in bar.get_children():
679 if c.kind == CursorKind.CALL_EXPR:
680 self.assertEqual(c.referenced.spelling, foo.spelling)
681 break
683 def test_mangled_name(self):
684 kInputForMangling = """\
685 int foo(int, int);
687 tu = get_tu(kInputForMangling, lang="cpp")
688 foo = get_cursor(tu, "foo")
690 # Since libclang does not link in targets, we cannot pass a triple to it
691 # and force the target. To enable this test to pass on all platforms, accept
692 # all valid manglings.
693 # [c-index-test handles this by running the source through clang, emitting
694 # an AST file and running libclang on that AST file]
695 self.assertIn(
696 foo.mangled_name, ("_Z3fooii", "__Z3fooii", "?foo@@YAHHH", "?foo@@YAHHH@Z")