1 from clang.cindex import CursorKind 2 from clang.cindex import TypeKind 3 from .util import get_cursor 4 from .util import get_tu 5 6 kInput = """\ 7 // FIXME: Find nicer way to drop builtins and other cruft. 8 int start_decl; 9 10 struct s0 { 11 int a; 12 int b; 13 }; 14 15 struct s1; 16 17 void f0(int a0, int a1) { 18 int l0, l1; 19 20 if (a0) 21 return; 22 23 for (;;) { 24 break; 25 } 26 } 27 """ 28 29 def test_get_children(): 30 tu = get_tu(kInput) 31 32 # Skip until past start_decl. 33 it = tu.cursor.get_children() 34 while it.next().spelling != 'start_decl': 35 pass 36 37 tu_nodes = list(it) 38 39 assert len(tu_nodes) == 3 40 41 assert tu_nodes[0] != tu_nodes[1] 42 assert tu_nodes[0].kind == CursorKind.STRUCT_DECL 43 assert tu_nodes[0].spelling == 's0' 44 assert tu_nodes[0].is_definition() == True 45 assert tu_nodes[0].location.file.name == 't.c' 46 assert tu_nodes[0].location.line == 4 47 assert tu_nodes[0].location.column == 8 48 assert tu_nodes[0].hash > 0 49 50 s0_nodes = list(tu_nodes[0].get_children()) 51 assert len(s0_nodes) == 2 52 assert s0_nodes[0].kind == CursorKind.FIELD_DECL 53 assert s0_nodes[0].spelling == 'a' 54 assert s0_nodes[0].type.kind == TypeKind.INT 55 assert s0_nodes[1].kind == CursorKind.FIELD_DECL 56 assert s0_nodes[1].spelling == 'b' 57 assert s0_nodes[1].type.kind == TypeKind.INT 58 59 assert tu_nodes[1].kind == CursorKind.STRUCT_DECL 60 assert tu_nodes[1].spelling == 's1' 61 assert tu_nodes[1].displayname == 's1' 62 assert tu_nodes[1].is_definition() == False 63 64 assert tu_nodes[2].kind == CursorKind.FUNCTION_DECL 65 assert tu_nodes[2].spelling == 'f0' 66 assert tu_nodes[2].displayname == 'f0(int, int)' 67 assert tu_nodes[2].is_definition() == True 68 69 def test_underlying_type(): 70 tu = get_tu('typedef int foo;') 71 typedef = get_cursor(tu, 'foo') 72 assert typedef is not None 73 74 assert typedef.kind.is_declaration() 75 underlying = typedef.underlying_typedef_type 76 assert underlying.kind == TypeKind.INT 77 78 def test_enum_type(): 79 tu = get_tu('enum TEST { FOO=1, BAR=2 };') 80 enum = get_cursor(tu, 'TEST') 81 assert enum is not None 82 83 assert enum.kind == CursorKind.ENUM_DECL 84 enum_type = enum.enum_type 85 assert enum_type.kind == TypeKind.UINT 86 87 def test_objc_type_encoding(): 88 tu = get_tu('int i;', lang='objc') 89 i = get_cursor(tu, 'i') 90 91 assert i is not None 92 assert i.objc_type_encoding == 'i' 93