| 1 |
import os |
| 2 |
from clang.cindex import Config |
| 3 |
if 'CLANG_LIBRARY_PATH' in os.environ: |
| 4 |
Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) |
| 5 |
|
| 6 |
import ctypes |
| 7 |
import gc |
| 8 |
import unittest |
| 9 |
|
| 10 |
from clang.cindex import AvailabilityKind |
| 11 |
from clang.cindex import CursorKind |
| 12 |
from clang.cindex import TemplateArgumentKind |
| 13 |
from clang.cindex import TranslationUnit |
| 14 |
from clang.cindex import TypeKind |
| 15 |
from .util import get_cursor |
| 16 |
from .util import get_cursors |
| 17 |
from .util import get_tu |
| 18 |
|
| 19 |
|
| 20 |
kInput = """\ |
| 21 |
struct s0 { |
| 22 |
int a; |
| 23 |
int b; |
| 24 |
}; |
| 25 |
|
| 26 |
struct s1; |
| 27 |
|
| 28 |
void f0(int a0, int a1) { |
| 29 |
int l0, l1; |
| 30 |
|
| 31 |
if (a0) |
| 32 |
return; |
| 33 |
|
| 34 |
for (;;) { |
| 35 |
break; |
| 36 |
} |
| 37 |
} |
| 38 |
""" |
| 39 |
|
| 40 |
kParentTest = """\ |
| 41 |
class C { |
| 42 |
void f(); |
| 43 |
} |
| 44 |
|
| 45 |
void C::f() { } |
| 46 |
""" |
| 47 |
|
| 48 |
kTemplateArgTest = """\ |
| 49 |
template <int kInt, typename T, bool kBool> |
| 50 |
void foo(); |
| 51 |
|
| 52 |
template<> |
| 53 |
void foo<-7, float, true>(); |
| 54 |
""" |
| 55 |
|
| 56 |
class TestCursor(unittest.TestCase): |
| 57 |
def test_get_children(self): |
| 58 |
tu = get_tu(kInput) |
| 59 |
|
| 60 |
it = tu.cursor.get_children() |
| 61 |
tu_nodes = list(it) |
| 62 |
|
| 63 |
self.assertEqual(len(tu_nodes), 3) |
| 64 |
for cursor in tu_nodes: |
| 65 |
self.assertIsNotNone(cursor.translation_unit) |
| 66 |
|
| 67 |
self.assertNotEqual(tu_nodes[0], tu_nodes[1]) |
| 68 |
self.assertEqual(tu_nodes[0].kind, CursorKind.STRUCT_DECL) |
| 69 |
self.assertEqual(tu_nodes[0].spelling, 's0') |
| 70 |
self.assertEqual(tu_nodes[0].is_definition(), True) |
| 71 |
self.assertEqual(tu_nodes[0].location.file.name, 't.c') |
| 72 |
self.assertEqual(tu_nodes[0].location.line, 1) |
| 73 |
self.assertEqual(tu_nodes[0].location.column, 8) |
| 74 |
self.assertGreater(tu_nodes[0].hash, 0) |
| 75 |
self.assertIsNotNone(tu_nodes[0].translation_unit) |
| 76 |
|
| 77 |
s0_nodes = list(tu_nodes[0].get_children()) |
| 78 |
self.assertEqual(len(s0_nodes), 2) |
| 79 |
self.assertEqual(s0_nodes[0].kind, CursorKind.FIELD_DECL) |
| 80 |
self.assertEqual(s0_nodes[0].spelling, 'a') |
| 81 |
self.assertEqual(s0_nodes[0].type.kind, TypeKind.INT) |
| 82 |
self.assertEqual(s0_nodes[1].kind, CursorKind.FIELD_DECL) |
| 83 |
self.assertEqual(s0_nodes[1].spelling, 'b') |
| 84 |
self.assertEqual(s0_nodes[1].type.kind, TypeKind.INT) |
| 85 |
|
| 86 |
self.assertEqual(tu_nodes[1].kind, CursorKind.STRUCT_DECL) |
| 87 |
self.assertEqual(tu_nodes[1].spelling, 's1') |
| 88 |
self.assertEqual(tu_nodes[1].displayname, 's1') |
| 89 |
self.assertEqual(tu_nodes[1].is_definition(), False) |
| 90 |
|
| 91 |
self.assertEqual(tu_nodes[2].kind, CursorKind.FUNCTION_DECL) |
| 92 |
self.assertEqual(tu_nodes[2].spelling, 'f0') |
| 93 |
self.assertEqual(tu_nodes[2].displayname, 'f0(int, int)') |
| 94 |
self.assertEqual(tu_nodes[2].is_definition(), True) |
| 95 |
|
| 96 |
def test_references(self): |
| 97 |
"""Ensure that references to TranslationUnit are kept.""" |
| 98 |
tu = get_tu('int x;') |
| 99 |
cursors = list(tu.cursor.get_children()) |
| 100 |
self.assertGreater(len(cursors), 0) |
| 101 |
|
| 102 |
cursor = cursors[0] |
| 103 |
self.assertIsInstance(cursor.translation_unit, TranslationUnit) |
| 104 |
|
| 105 |
# Delete reference to TU and perform a full GC. |
| 106 |
del tu |
| 107 |
gc.collect() |
| 108 |
self.assertIsInstance(cursor.translation_unit, TranslationUnit) |
| 109 |
|
| 110 |
# If the TU was destroyed, this should cause a segfault. |
| 111 |
parent = cursor.semantic_parent |
| 112 |
|
| 113 |
def test_canonical(self): |
| 114 |
source = 'struct X; struct X; struct X { int member; };' |
| 115 |
tu = get_tu(source) |
| 116 |
|
| 117 |
cursors = [] |
| 118 |
for cursor in tu.cursor.get_children(): |
| 119 |
if cursor.spelling == 'X': |
| 120 |
cursors.append(cursor) |
| 121 |
|
| 122 |
self.assertEqual(len(cursors), 3) |
| 123 |
self.assertEqual(cursors[1].canonical, cursors[2].canonical) |
| 124 |
|
| 125 |
def test_is_const_method(self): |
| 126 |
"""Ensure Cursor.is_const_method works.""" |
| 127 |
source = 'class X { void foo() const; void bar(); };' |
| 128 |
tu = get_tu(source, lang='cpp') |
| 129 |
|
| 130 |
cls = get_cursor(tu, 'X') |
| 131 |
foo = get_cursor(tu, 'foo') |
| 132 |
bar = get_cursor(tu, 'bar') |
| 133 |
self.assertIsNotNone(cls) |
| 134 |
self.assertIsNotNone(foo) |
| 135 |
self.assertIsNotNone(bar) |
| 136 |
|
| 137 |
self.assertTrue(foo.is_const_method()) |
| 138 |
self.assertFalse(bar.is_const_method()) |
| 139 |
|
| 140 |
def test_is_converting_constructor(self): |
| 141 |
"""Ensure Cursor.is_converting_constructor works.""" |
| 142 |
source = 'class X { explicit X(int); X(double); X(); };' |
| 143 |
tu = get_tu(source, lang='cpp') |
| 144 |
|
| 145 |
xs = get_cursors(tu, 'X') |
| 146 |
|
| 147 |
self.assertEqual(len(xs), 4) |
| 148 |
self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL) |
| 149 |
cs = xs[1:] |
| 150 |
self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR) |
| 151 |
self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR) |
| 152 |
self.assertEqual(cs[2].kind, CursorKind.CONSTRUCTOR) |
| 153 |
|
| 154 |
self.assertFalse(cs[0].is_converting_constructor()) |
| 155 |
self.assertTrue(cs[1].is_converting_constructor()) |
| 156 |
self.assertFalse(cs[2].is_converting_constructor()) |
| 157 |
|
| 158 |
|
| 159 |
def test_is_copy_constructor(self): |
| 160 |
"""Ensure Cursor.is_copy_constructor works.""" |
| 161 |
source = 'class X { X(); X(const X&); X(X&&); };' |
| 162 |
tu = get_tu(source, lang='cpp') |
| 163 |
|
| 164 |
xs = get_cursors(tu, 'X') |
| 165 |
self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL) |
| 166 |
cs = xs[1:] |
| 167 |
self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR) |
| 168 |
self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR) |
| 169 |
self.assertEqual(cs[2].kind, CursorKind.CONSTRUCTOR) |
| 170 |
|
| 171 |
self.assertFalse(cs[0].is_copy_constructor()) |
| 172 |
self.assertTrue(cs[1].is_copy_constructor()) |
| 173 |
self.assertFalse(cs[2].is_copy_constructor()) |
| 174 |
|
| 175 |
def test_is_default_constructor(self): |
| 176 |
"""Ensure Cursor.is_default_constructor works.""" |
| 177 |
source = 'class X { X(); X(int); };' |
| 178 |
tu = get_tu(source, lang='cpp') |
| 179 |
|
| 180 |
xs = get_cursors(tu, 'X') |
| 181 |
self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL) |
| 182 |
cs = xs[1:] |
| 183 |
self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR) |
| 184 |
self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR) |
| 185 |
|
| 186 |
self.assertTrue(cs[0].is_default_constructor()) |
| 187 |
self.assertFalse(cs[1].is_default_constructor()) |
| 188 |
|
| 189 |
def test_is_move_constructor(self): |
| 190 |
"""Ensure Cursor.is_move_constructor works.""" |
| 191 |
source = 'class X { X(); X(const X&); X(X&&); };' |
| 192 |
tu = get_tu(source, lang='cpp') |
| 193 |
|
| 194 |
xs = get_cursors(tu, 'X') |
| 195 |
self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL) |
| 196 |
cs = xs[1:] |
| 197 |
self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR) |
| 198 |
self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR) |
| 199 |
self.assertEqual(cs[2].kind, CursorKind.CONSTRUCTOR) |
| 200 |
|
| 201 |
self.assertFalse(cs[0].is_move_constructor()) |
| 202 |
self.assertFalse(cs[1].is_move_constructor()) |
| 203 |
self.assertTrue(cs[2].is_move_constructor()) |
| 204 |
|
| 205 |
def test_is_default_method(self): |
| 206 |
"""Ensure Cursor.is_default_method works.""" |
| 207 |
source = 'class X { X() = default; }; class Y { Y(); };' |
| 208 |
tu = get_tu(source, lang='cpp') |
| 209 |
|
| 210 |
xs = get_cursors(tu, 'X') |
| 211 |
ys = get_cursors(tu, 'Y') |
| 212 |
|
| 213 |
self.assertEqual(len(xs), 2) |
| 214 |
self.assertEqual(len(ys), 2) |
| 215 |
|
| 216 |
xc = xs[1] |
| 217 |
yc = ys[1] |
| 218 |
|
| 219 |
self.assertTrue(xc.is_default_method()) |
| 220 |
self.assertFalse(yc.is_default_method()) |
| 221 |
|
| 222 |
def test_is_move_assignment_operator_method(self): |
| 223 |
"""Ensure Cursor.is_move_assignment_operator_method works.""" |
| 224 |
source_with_move_assignment_operators = """ |
| 225 |
struct Foo { |
| 226 |
// Those are move-assignment operators |
| 227 |
bool operator=(const Foo&&); |
| 228 |
bool operator=(Foo&&); |
| 229 |
bool operator=(volatile Foo&&); |
| 230 |
bool operator=(const volatile Foo&&); |
| 231 |
|
| 232 |
// Positive-check that the recognition works for templated classes too |
| 233 |
template <typename T> |
| 234 |
class Bar { |
| 235 |
bool operator=(const Bar&&); |
| 236 |
bool operator=(Bar<T>&&); |
| 237 |
bool operator=(volatile Bar&&); |
| 238 |
bool operator=(const volatile Bar<T>&&); |
| 239 |
}; |
| 240 |
""" |
| 241 |
source_without_move_assignment_operators = """ |
| 242 |
struct Foo { |
| 243 |
// Those are not move-assignment operators |
| 244 |
template<typename T> |
| 245 |
bool operator=(const T&&); |
| 246 |
bool operator=(const bool&&); |
| 247 |
bool operator=(char&&); |
| 248 |
bool operator=(volatile unsigned int&&); |
| 249 |
bool operator=(const volatile unsigned char&&); |
| 250 |
bool operator=(int); |
| 251 |
bool operator=(Foo); |
| 252 |
}; |
| 253 |
""" |
| 254 |
tu_with_move_assignment_operators = get_tu( |
| 255 |
source_with_move_assignment_operators, lang="cpp" |
| 256 |
) |
| 257 |
tu_without_move_assignment_operators = get_tu( |
| 258 |
source_without_move_assignment_operators, lang="cpp" |
| 259 |
) |
| 260 |
|
| 261 |
move_assignment_operators_cursors = get_cursors( |
| 262 |
tu_with_move_assignment_operators, "operator=" |
| 263 |
) |
| 264 |
non_move_assignment_operators_cursors = get_cursors( |
| 265 |
tu_without_move_assignment_operators, "operator=" |
| 266 |
) |
| 267 |
|
| 268 |
self.assertEqual(len(move_assignment_operators_cursors), 8) |
| 269 |
self.assertTrue(len(non_move_assignment_operators_cursors), 7) |
| 270 |
|
| 271 |
self.assertTrue(all([ |
| 272 |
cursor.is_move_assignment_operator_method() |
| 273 |
for cursor in move_assignment_operators_cursors |
| 274 |
])) |
| 275 |
self.assertFalse(any([ |
| 276 |
cursor.is_move_assignment_operator_method() |
| 277 |
for cursor in non_move_assignment_operators_cursors |
| 278 |
])) |
| 279 |
|
| 280 |
def test_is_mutable_field(self): |
| 281 |
"""Ensure Cursor.is_mutable_field works.""" |
| 282 |
source = 'class X { int x_; mutable int y_; };' |
| 283 |
tu = get_tu(source, lang='cpp') |
| 284 |
|
| 285 |
cls = get_cursor(tu, 'X') |
| 286 |
x_ = get_cursor(tu, 'x_') |
| 287 |
y_ = get_cursor(tu, 'y_') |
| 288 |
self.assertIsNotNone(cls) |
| 289 |
self.assertIsNotNone(x_) |
| 290 |
self.assertIsNotNone(y_) |
| 291 |
|
| 292 |
self.assertFalse(x_.is_mutable_field()) |
| 293 |
self.assertTrue(y_.is_mutable_field()) |
| 294 |
|
| 295 |
def test_is_static_method(self): |
| 296 |
"""Ensure Cursor.is_static_method works.""" |
| 297 |
|
| 298 |
source = 'class X { static void foo(); void bar(); };' |
| 299 |
tu = get_tu(source, lang='cpp') |
| 300 |
|
| 301 |
cls = get_cursor(tu, 'X') |
| 302 |
foo = get_cursor(tu, 'foo') |
| 303 |
bar = get_cursor(tu, 'bar') |
| 304 |
self.assertIsNotNone(cls) |
| 305 |
self.assertIsNotNone(foo) |
| 306 |
self.assertIsNotNone(bar) |
| 307 |
|
| 308 |
self.assertTrue(foo.is_static_method()) |
| 309 |
self.assertFalse(bar.is_static_method()) |
| 310 |
|
| 311 |
def test_is_pure_virtual_method(self): |
| 312 |
"""Ensure Cursor.is_pure_virtual_method works.""" |
| 313 |
source = 'class X { virtual void foo() = 0; virtual void bar(); };' |
| 314 |
tu = get_tu(source, lang='cpp') |
| 315 |
|
| 316 |
cls = get_cursor(tu, 'X') |
| 317 |
foo = get_cursor(tu, 'foo') |
| 318 |
bar = get_cursor(tu, 'bar') |
| 319 |
self.assertIsNotNone(cls) |
| 320 |
self.assertIsNotNone(foo) |
| 321 |
self.assertIsNotNone(bar) |
| 322 |
|
| 323 |
self.assertTrue(foo.is_pure_virtual_method()) |
| 324 |
self.assertFalse(bar.is_pure_virtual_method()) |
| 325 |
|
| 326 |
def test_is_virtual_method(self): |
| 327 |
"""Ensure Cursor.is_virtual_method works.""" |
| 328 |
source = 'class X { virtual void foo(); void bar(); };' |
| 329 |
tu = get_tu(source, lang='cpp') |
| 330 |
|
| 331 |
cls = get_cursor(tu, 'X') |
| 332 |
foo = get_cursor(tu, 'foo') |
| 333 |
bar = get_cursor(tu, 'bar') |
| 334 |
self.assertIsNotNone(cls) |
| 335 |
self.assertIsNotNone(foo) |
| 336 |
self.assertIsNotNone(bar) |
| 337 |
|
| 338 |
self.assertTrue(foo.is_virtual_method()) |
| 339 |
self.assertFalse(bar.is_virtual_method()) |
| 340 |
|
| 341 |
def test_is_abstract_record(self): |
| 342 |
"""Ensure Cursor.is_abstract_record works.""" |
| 343 |
source = 'struct X { virtual void x() = 0; }; struct Y : X { void x(); };' |
| 344 |
tu = get_tu(source, lang='cpp') |
| 345 |
|
| 346 |
cls = get_cursor(tu, 'X') |
| 347 |
self.assertTrue(cls.is_abstract_record()) |
| 348 |
|
| 349 |
cls = get_cursor(tu, 'Y') |
| 350 |
self.assertFalse(cls.is_abstract_record()) |
| 351 |
|
| 352 |
def test_is_scoped_enum(self): |
| 353 |
"""Ensure Cursor.is_scoped_enum works.""" |
| 354 |
source = 'class X {}; enum RegularEnum {}; enum class ScopedEnum {};' |
| 355 |
tu = get_tu(source, lang='cpp') |
| 356 |
|
| 357 |
cls = get_cursor(tu, 'X') |
| 358 |
regular_enum = get_cursor(tu, 'RegularEnum') |
| 359 |
scoped_enum = get_cursor(tu, 'ScopedEnum') |
| 360 |
self.assertIsNotNone(cls) |
| 361 |
self.assertIsNotNone(regular_enum) |
| 362 |
self.assertIsNotNone(scoped_enum) |
| 363 |
|
| 364 |
self.assertFalse(cls.is_scoped_enum()) |
| 365 |
self.assertFalse(regular_enum.is_scoped_enum()) |
| 366 |
self.assertTrue(scoped_enum.is_scoped_enum()) |
| 367 |
|
| 368 |
def test_underlying_type(self): |
| 369 |
tu = get_tu('typedef int foo;') |
| 370 |
typedef = get_cursor(tu, 'foo') |
| 371 |
self.assertIsNotNone(typedef) |
| 372 |
|
| 373 |
self.assertTrue(typedef.kind.is_declaration()) |
| 374 |
underlying = typedef.underlying_typedef_type |
| 375 |
self.assertEqual(underlying.kind, TypeKind.INT) |
| 376 |
|
| 377 |
def test_semantic_parent(self): |
| 378 |
tu = get_tu(kParentTest, 'cpp') |
| 379 |
curs = get_cursors(tu, 'f') |
| 380 |
decl = get_cursor(tu, 'C') |
| 381 |
self.assertEqual(len(curs), 2) |
| 382 |
self.assertEqual(curs[0].semantic_parent, curs[1].semantic_parent) |
| 383 |
self.assertEqual(curs[0].semantic_parent, decl) |
| 384 |
|
| 385 |
def test_lexical_parent(self): |
| 386 |
tu = get_tu(kParentTest, 'cpp') |
| 387 |
curs = get_cursors(tu, 'f') |
| 388 |
decl = get_cursor(tu, 'C') |
| 389 |
self.assertEqual(len(curs), 2) |
| 390 |
self.assertNotEqual(curs[0].lexical_parent, curs[1].lexical_parent) |
| 391 |
self.assertEqual(curs[0].lexical_parent, decl) |
| 392 |
self.assertEqual(curs[1].lexical_parent, tu.cursor) |
| 393 |
|
| 394 |
def test_enum_type(self): |
| 395 |
tu = get_tu('enum TEST { FOO=1, BAR=2 };') |
| 396 |
enum = get_cursor(tu, 'TEST') |
| 397 |
self.assertIsNotNone(enum) |
| 398 |
|
| 399 |
self.assertEqual(enum.kind, CursorKind.ENUM_DECL) |
| 400 |
enum_type = enum.enum_type |
| 401 |
self.assertIn(enum_type.kind, (TypeKind.UINT, TypeKind.INT)) |
| 402 |
|
| 403 |
def test_enum_type_cpp(self): |
| 404 |
tu = get_tu('enum TEST : long long { FOO=1, BAR=2 };', lang="cpp") |
| 405 |
enum = get_cursor(tu, 'TEST') |
| 406 |
self.assertIsNotNone(enum) |
| 407 |
|
| 408 |
self.assertEqual(enum.kind, CursorKind.ENUM_DECL) |
| 409 |
self.assertEqual(enum.enum_type.kind, TypeKind.LONGLONG) |
| 410 |
|
| 411 |
def test_objc_type_encoding(self): |
| 412 |
tu = get_tu('int i;', lang='objc') |
| 413 |
i = get_cursor(tu, 'i') |
| 414 |
|
| 415 |
self.assertIsNotNone(i) |
| 416 |
self.assertEqual(i.objc_type_encoding, 'i') |
| 417 |
|
| 418 |
def test_enum_values(self): |
| 419 |
tu = get_tu('enum TEST { SPAM=1, EGG, HAM = EGG * 20};') |
| 420 |
enum = get_cursor(tu, 'TEST') |
| 421 |
self.assertIsNotNone(enum) |
| 422 |
|
| 423 |
self.assertEqual(enum.kind, CursorKind.ENUM_DECL) |
| 424 |
|
| 425 |
enum_constants = list(enum.get_children()) |
| 426 |
self.assertEqual(len(enum_constants), 3) |
| 427 |
|
| 428 |
spam, egg, ham = enum_constants |
| 429 |
|
| 430 |
self.assertEqual(spam.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 431 |
self.assertEqual(spam.enum_value, 1) |
| 432 |
self.assertEqual(egg.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 433 |
self.assertEqual(egg.enum_value, 2) |
| 434 |
self.assertEqual(ham.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 435 |
self.assertEqual(ham.enum_value, 40) |
| 436 |
|
| 437 |
def test_enum_values_cpp(self): |
| 438 |
tu = get_tu('enum TEST : long long { SPAM = -1, HAM = 0x10000000000};', lang="cpp") |
| 439 |
enum = get_cursor(tu, 'TEST') |
| 440 |
self.assertIsNotNone(enum) |
| 441 |
|
| 442 |
self.assertEqual(enum.kind, CursorKind.ENUM_DECL) |
| 443 |
|
| 444 |
enum_constants = list(enum.get_children()) |
| 445 |
self.assertEqual(len(enum_constants), 2) |
| 446 |
|
| 447 |
spam, ham = enum_constants |
| 448 |
|
| 449 |
self.assertEqual(spam.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 450 |
self.assertEqual(spam.enum_value, -1) |
| 451 |
self.assertEqual(ham.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 452 |
self.assertEqual(ham.enum_value, 0x10000000000) |
| 453 |
|
| 454 |
def test_annotation_attribute(self): |
| 455 |
tu = get_tu('int foo (void) __attribute__ ((annotate("here be annotation attribute")));') |
| 456 |
|
| 457 |
foo = get_cursor(tu, 'foo') |
| 458 |
self.assertIsNotNone(foo) |
| 459 |
|
| 460 |
for c in foo.get_children(): |
| 461 |
if c.kind == CursorKind.ANNOTATE_ATTR: |
| 462 |
self.assertEqual(c.displayname, "here be annotation attribute") |
| 463 |
break |
| 464 |
else: |
| 465 |
self.fail("Couldn't find annotation") |
| 466 |
|
| 467 |
def test_annotation_template(self): |
| 468 |
annotation = '__attribute__ ((annotate("annotation")))' |
| 469 |
for source, kind in [ |
| 470 |
('int foo (T value) %s;', CursorKind.FUNCTION_TEMPLATE), |
| 471 |
('class %s foo {};', CursorKind.CLASS_TEMPLATE), |
| 472 |
]: |
| 473 |
source = 'template<typename T> ' + (source % annotation) |
| 474 |
tu = get_tu(source, lang="cpp") |
| 475 |
|
| 476 |
foo = get_cursor(tu, 'foo') |
| 477 |
self.assertIsNotNone(foo) |
| 478 |
self.assertEqual(foo.kind, kind) |
| 479 |
|
| 480 |
for c in foo.get_children(): |
| 481 |
if c.kind == CursorKind.ANNOTATE_ATTR: |
| 482 |
self.assertEqual(c.displayname, "annotation") |
| 483 |
break |
| 484 |
else: |
| 485 |
self.fail("Couldn't find annotation for {}".format(kind)) |
| 486 |
|
| 487 |
def test_result_type(self): |
| 488 |
tu = get_tu('int foo();') |
| 489 |
foo = get_cursor(tu, 'foo') |
| 490 |
|
| 491 |
self.assertIsNotNone(foo) |
| 492 |
t = foo.result_type |
| 493 |
self.assertEqual(t.kind, TypeKind.INT) |
| 494 |
|
| 495 |
def test_result_type_objc_method_decl(self): |
| 496 |
code = """\ |
| 497 |
@interface Interface : NSObject |
| 498 |
-(void)voidMethod; |
| 499 |
@end |
| 500 |
""" |
| 501 |
tu = get_tu(code, lang='objc') |
| 502 |
cursor = get_cursor(tu, 'voidMethod') |
| 503 |
result_type = cursor.result_type |
| 504 |
self.assertEqual(cursor.kind, CursorKind.OBJC_INSTANCE_METHOD_DECL) |
| 505 |
self.assertEqual(result_type.kind, TypeKind.VOID) |
| 506 |
|
| 507 |
def test_availability(self): |
| 508 |
tu = get_tu('class A { A(A const&) = delete; };', lang='cpp') |
| 509 |
|
| 510 |
# AvailabilityKind.AVAILABLE |
| 511 |
cursor = get_cursor(tu, 'A') |
| 512 |
self.assertEqual(cursor.kind, CursorKind.CLASS_DECL) |
| 513 |
self.assertEqual(cursor.availability, AvailabilityKind.AVAILABLE) |
| 514 |
|
| 515 |
# AvailabilityKind.NOT_AVAILABLE |
| 516 |
cursors = get_cursors(tu, 'A') |
| 517 |
for c in cursors: |
| 518 |
if c.kind == CursorKind.CONSTRUCTOR: |
| 519 |
self.assertEqual(c.availability, AvailabilityKind.NOT_AVAILABLE) |
| 520 |
break |
| 521 |
else: |
| 522 |
self.fail("Could not find cursor for deleted constructor") |
| 523 |
|
| 524 |
# AvailabilityKind.DEPRECATED |
| 525 |
tu = get_tu('void test() __attribute__((deprecated));', lang='cpp') |
| 526 |
cursor = get_cursor(tu, 'test') |
| 527 |
self.assertEqual(cursor.availability, AvailabilityKind.DEPRECATED) |
| 528 |
|
| 529 |
# AvailabilityKind.NOT_ACCESSIBLE is only used in the code completion results |
| 530 |
|
| 531 |
def test_get_tokens(self): |
| 532 |
"""Ensure we can map cursors back to tokens.""" |
| 533 |
tu = get_tu('int foo(int i);') |
| 534 |
foo = get_cursor(tu, 'foo') |
| 535 |
|
| 536 |
tokens = list(foo.get_tokens()) |
| 537 |
self.assertEqual(len(tokens), 6) |
| 538 |
self.assertEqual(tokens[0].spelling, 'int') |
| 539 |
self.assertEqual(tokens[1].spelling, 'foo') |
| 540 |
|
| 541 |
def test_get_token_cursor(self): |
| 542 |
"""Ensure we can map tokens to cursors.""" |
| 543 |
tu = get_tu('class A {}; int foo(A var = A());', lang='cpp') |
| 544 |
foo = get_cursor(tu, 'foo') |
| 545 |
|
| 546 |
for cursor in foo.walk_preorder(): |
| 547 |
if cursor.kind.is_expression() and not cursor.kind.is_statement(): |
| 548 |
break |
| 549 |
else: |
| 550 |
self.fail("Could not find default value expression") |
| 551 |
|
| 552 |
tokens = list(cursor.get_tokens()) |
| 553 |
self.assertEqual(len(tokens), 4, [t.spelling for t in tokens]) |
| 554 |
self.assertEqual(tokens[0].spelling, '=') |
| 555 |
self.assertEqual(tokens[1].spelling, 'A') |
| 556 |
self.assertEqual(tokens[2].spelling, '(') |
| 557 |
self.assertEqual(tokens[3].spelling, ')') |
| 558 |
t_cursor = tokens[1].cursor |
| 559 |
self.assertEqual(t_cursor.kind, CursorKind.TYPE_REF) |
| 560 |
r_cursor = t_cursor.referenced # should not raise an exception |
| 561 |
self.assertEqual(r_cursor.kind, CursorKind.CLASS_DECL) |
| 562 |
|
| 563 |
def test_get_arguments(self): |
| 564 |
tu = get_tu('void foo(int i, int j);') |
| 565 |
foo = get_cursor(tu, 'foo') |
| 566 |
arguments = list(foo.get_arguments()) |
| 567 |
|
| 568 |
self.assertEqual(len(arguments), 2) |
| 569 |
self.assertEqual(arguments[0].spelling, "i") |
| 570 |
self.assertEqual(arguments[1].spelling, "j") |
| 571 |
|
| 572 |
def test_get_num_template_arguments(self): |
| 573 |
tu = get_tu(kTemplateArgTest, lang='cpp') |
| 574 |
foos = get_cursors(tu, 'foo') |
| 575 |
|
| 576 |
self.assertEqual(foos[1].get_num_template_arguments(), 3) |
| 577 |
|
| 578 |
def test_get_template_argument_kind(self): |
| 579 |
tu = get_tu(kTemplateArgTest, lang='cpp') |
| 580 |
foos = get_cursors(tu, 'foo') |
| 581 |
|
| 582 |
self.assertEqual(foos[1].get_template_argument_kind(0), TemplateArgumentKind.INTEGRAL) |
| 583 |
self.assertEqual(foos[1].get_template_argument_kind(1), TemplateArgumentKind.TYPE) |
| 584 |
self.assertEqual(foos[1].get_template_argument_kind(2), TemplateArgumentKind.INTEGRAL) |
| 585 |
|
| 586 |
def test_get_template_argument_type(self): |
| 587 |
tu = get_tu(kTemplateArgTest, lang='cpp') |
| 588 |
foos = get_cursors(tu, 'foo') |
| 589 |
|
| 590 |
self.assertEqual(foos[1].get_template_argument_type(1).kind, TypeKind.FLOAT) |
| 591 |
|
| 592 |
def test_get_template_argument_value(self): |
| 593 |
tu = get_tu(kTemplateArgTest, lang='cpp') |
| 594 |
foos = get_cursors(tu, 'foo') |
| 595 |
|
| 596 |
self.assertEqual(foos[1].get_template_argument_value(0), -7) |
| 597 |
self.assertEqual(foos[1].get_template_argument_value(2), True) |
| 598 |
|
| 599 |
def test_get_template_argument_unsigned_value(self): |
| 600 |
tu = get_tu(kTemplateArgTest, lang='cpp') |
| 601 |
foos = get_cursors(tu, 'foo') |
| 602 |
|
| 603 |
self.assertEqual(foos[1].get_template_argument_unsigned_value(0), 2 ** 32 - 7) |
| 604 |
self.assertEqual(foos[1].get_template_argument_unsigned_value(2), True) |
| 605 |
|
| 606 |
def test_referenced(self): |
| 607 |
tu = get_tu('void foo(); void bar() { foo(); }') |
| 608 |
foo = get_cursor(tu, 'foo') |
| 609 |
bar = get_cursor(tu, 'bar') |
| 610 |
for c in bar.get_children(): |
| 611 |
if c.kind == CursorKind.CALL_EXPR: |
| 612 |
self.assertEqual(c.referenced.spelling, foo.spelling) |
| 613 |
break |
| 614 |
|
| 615 |
def test_mangled_name(self): |
| 616 |
kInputForMangling = """\ |
| 617 |
int foo(int, int); |
| 618 |
""" |
| 619 |
tu = get_tu(kInputForMangling, lang='cpp') |
| 620 |
foo = get_cursor(tu, 'foo') |
| 621 |
|
| 622 |
# Since libclang does not link in targets, we cannot pass a triple to it |
| 623 |
# and force the target. To enable this test to pass on all platforms, accept |
| 624 |
# all valid manglings. |
| 625 |
# [c-index-test handles this by running the source through clang, emitting |
| 626 |
# an AST file and running libclang on that AST file] |
| 627 |
self.assertIn(foo.mangled_name, ('_Z3fooii', '__Z3fooii', '?foo@@YAHHH', '?foo@@YAHHH@Z')) |