Gregory Szorc | 8605760 | 2012-02-17 07:44:46 +0000 | [diff] [blame] | 1 | from clang.cindex import CursorKind |
Gregory Szorc | 8605760 | 2012-02-17 07:44:46 +0000 | [diff] [blame] | 2 | from clang.cindex import TypeKind |
Gregory Szorc | 8605760 | 2012-02-17 07:44:46 +0000 | [diff] [blame] | 3 | from nose.tools import raises |
Gregory Szorc | 1f1988f | 2012-03-10 22:19:05 +0000 | [diff] [blame] | 4 | from .util import get_cursor |
| 5 | from .util import get_tu |
Argyrios Kyrtzidis | 2312f5f | 2011-08-17 17:01:10 +0000 | [diff] [blame] | 6 | |
| 7 | kInput = """\ |
| 8 | |
| 9 | typedef int I; |
| 10 | |
| 11 | struct 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 Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame] | 24 | def test_a_struct(): |
| 25 | tu = get_tu(kInput) |
Argyrios Kyrtzidis | 2312f5f | 2011-08-17 17:01:10 +0000 | [diff] [blame] | 26 | |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 27 | 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 Kyrtzidis | 2312f5f | 2011-08-17 17:01:10 +0000 | [diff] [blame] | 31 | |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 32 | 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 Kyrtzidis | 2312f5f | 2011-08-17 17:01:10 +0000 | [diff] [blame] | 36 | |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 37 | 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 Kyrtzidis | 2312f5f | 2011-08-17 17:01:10 +0000 | [diff] [blame] | 42 | |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 43 | 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 Kyrtzidis | 2312f5f | 2011-08-17 17:01:10 +0000 | [diff] [blame] | 47 | |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 48 | 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 Kyrtzidis | 2312f5f | 2011-08-17 17:01:10 +0000 | [diff] [blame] | 52 | |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 53 | 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 Kyrtzidis | 2312f5f | 2011-08-17 17:01:10 +0000 | [diff] [blame] | 57 | |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 58 | 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 Kyrtzidis | 2312f5f | 2011-08-17 17:01:10 +0000 | [diff] [blame] | 62 | |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 63 | 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 Kyrtzidis | 2312f5f | 2011-08-17 17:01:10 +0000 | [diff] [blame] | 67 | |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 68 | 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 Gregor | 38d2d55 | 2011-10-19 05:50:34 +0000 | [diff] [blame] | 74 | |
| 75 | constarrayInput=""" |
| 76 | struct teststruct { |
| 77 | void *A[2]; |
| 78 | }; |
| 79 | """ |
| 80 | def testConstantArray(): |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame] | 81 | tu = get_tu(constarrayInput) |
Douglas Gregor | 38d2d55 | 2011-10-19 05:50:34 +0000 | [diff] [blame] | 82 | |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 83 | 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 Szorc | 96ad633 | 2012-02-05 19:42:06 +0000 | [diff] [blame] | 91 | |
Gregory Szorc | 7eb691a | 2012-02-20 17:44:49 +0000 | [diff] [blame] | 92 | def test_equal(): |
| 93 | """Ensure equivalence operators work on Type.""" |
| 94 | source = 'int a; int b; void *v;' |
| 95 | tu = get_tu(source) |
| 96 | |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 97 | a = get_cursor(tu, 'a') |
| 98 | b = get_cursor(tu, 'b') |
| 99 | v = get_cursor(tu, 'v') |
Gregory Szorc | 7eb691a | 2012-02-20 17:44:49 +0000 | [diff] [blame] | 100 | |
| 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 Szorc | 6e67eed | 2012-04-15 18:51:10 +0000 | [diff] [blame] | 111 | def 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 Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 119 | def 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) |
| 143 | def 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) |
| 155 | def 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) |
| 164 | def 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) |
| 173 | def 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 Szorc | 96ad633 | 2012-02-05 19:42:06 +0000 | [diff] [blame] | 181 | def test_is_pod(): |
Gregory Szorc | 8261345 | 2012-02-20 17:58:40 +0000 | [diff] [blame] | 182 | """Ensure Type.is_pod() works.""" |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame] | 183 | tu = get_tu('int i; void f();') |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 184 | i = get_cursor(tu, 'i') |
| 185 | f = get_cursor(tu, 'f') |
Gregory Szorc | 96ad633 | 2012-02-05 19:42:06 +0000 | [diff] [blame] | 186 | |
| 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 Szorc | 8605760 | 2012-02-17 07:44:46 +0000 | [diff] [blame] | 192 | |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame] | 193 | def test_function_variadic(): |
| 194 | """Ensure Type.is_function_variadic works.""" |
Gregory Szorc | 8605760 | 2012-02-17 07:44:46 +0000 | [diff] [blame] | 195 | |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame] | 196 | source =""" |
| 197 | #include <stdarg.h> |
| 198 | |
| 199 | void foo(int a, ...); |
| 200 | void bar(int a, int b); |
| 201 | """ |
| 202 | |
| 203 | tu = get_tu(source) |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 204 | foo = get_cursor(tu, 'foo') |
| 205 | bar = get_cursor(tu, 'bar') |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame] | 206 | |
| 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 | |
| 214 | def test_element_type(): |
Gregory Szorc | 8261345 | 2012-02-20 17:58:40 +0000 | [diff] [blame] | 215 | """Ensure Type.element_type works.""" |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame] | 216 | tu = get_tu('int i[5];') |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 217 | i = get_cursor(tu, 'i') |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame] | 218 | assert i is not None |
| 219 | |
Gregory Szorc | 8605760 | 2012-02-17 07:44:46 +0000 | [diff] [blame] | 220 | assert i.type.kind == TypeKind.CONSTANTARRAY |
| 221 | assert i.type.element_type.kind == TypeKind.INT |
| 222 | |
| 223 | @raises(Exception) |
| 224 | def test_invalid_element_type(): |
| 225 | """Ensure Type.element_type raises if type doesn't have elements.""" |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame] | 226 | tu = get_tu('int i;') |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 227 | i = get_cursor(tu, 'i') |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame] | 228 | assert i is not None |
Gregory Szorc | 8605760 | 2012-02-17 07:44:46 +0000 | [diff] [blame] | 229 | i.element_type |
Gregory Szorc | bf8ca00 | 2012-02-17 07:47:38 +0000 | [diff] [blame] | 230 | |
| 231 | def test_element_count(): |
Gregory Szorc | 8261345 | 2012-02-20 17:58:40 +0000 | [diff] [blame] | 232 | """Ensure Type.element_count works.""" |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame] | 233 | tu = get_tu('int i[5]; int j;') |
Gregory Szorc | 826fce5 | 2012-02-20 17:45:30 +0000 | [diff] [blame] | 234 | i = get_cursor(tu, 'i') |
| 235 | j = get_cursor(tu, 'j') |
Gregory Szorc | bf8ca00 | 2012-02-17 07:47:38 +0000 | [diff] [blame] | 236 | |
| 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 Szorc | 0e1f4f8 | 2012-02-20 17:58:02 +0000 | [diff] [blame] | 247 | |
| 248 | def 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 | |
| 263 | def test_is_restrict_qualified(): |
| 264 | """Ensure Type.is_restrict_qualified works.""" |
| 265 | |
Anders Waldenborg | e1f61c0 | 2012-05-02 19:35:37 +0000 | [diff] [blame^] | 266 | tu = get_tu('struct s { void * restrict i; void * j; };') |
Gregory Szorc | 0e1f4f8 | 2012-02-20 17:58:02 +0000 | [diff] [blame] | 267 | |
| 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() |