| 1 |
from clang.cindex import ( |
| 2 |
AvailabilityKind, |
| 3 |
BinaryOperator, |
| 4 |
Cursor, |
| 5 |
CursorKind, |
| 6 |
PrintingPolicy, |
| 7 |
PrintingPolicyProperty, |
| 8 |
StorageClass, |
| 9 |
TemplateArgumentKind, |
| 10 |
TranslationUnit, |
| 11 |
TypeKind, |
| 12 |
conf, |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
import gc |
| 17 |
import unittest |
| 18 |
|
| 19 |
from .util import get_cursor, get_cursors, get_tu |
| 20 |
|
| 21 |
CHILDREN_TEST = """\ |
| 22 |
struct s0 { |
| 23 |
int a; |
| 24 |
int b; |
| 25 |
}; |
| 26 |
|
| 27 |
struct s1; |
| 28 |
|
| 29 |
void f0(int a0, int a1) { |
| 30 |
int l0, l1; |
| 31 |
|
| 32 |
if (a0) |
| 33 |
return; |
| 34 |
|
| 35 |
for (;;) { |
| 36 |
break; |
| 37 |
} |
| 38 |
} |
| 39 |
""" |
| 40 |
|
| 41 |
PARENT_TEST = """\ |
| 42 |
class C { |
| 43 |
void f(); |
| 44 |
} |
| 45 |
|
| 46 |
void C::f() { } |
| 47 |
""" |
| 48 |
|
| 49 |
TEMPLATE_ARG_TEST = """\ |
| 50 |
template <int kInt, typename T, bool kBool> |
| 51 |
void foo(); |
| 52 |
|
| 53 |
template<> |
| 54 |
void foo<-7, float, true>(); |
| 55 |
""" |
| 56 |
|
| 57 |
BINOPS = """\ |
| 58 |
struct C { |
| 59 |
int m; |
| 60 |
}; |
| 61 |
|
| 62 |
void func(void){ |
| 63 |
int a, b; |
| 64 |
int C::* p = &C:: |
| 65 |
|
| 66 |
C c; |
| 67 |
c.*p; |
| 68 |
|
| 69 |
C* pc; |
| 70 |
pc->*p; |
| 71 |
|
| 72 |
a * b; |
| 73 |
a / b; |
| 74 |
a % b; |
| 75 |
a + b; |
| 76 |
a - b; |
| 77 |
|
| 78 |
a << b; |
| 79 |
a >> b; |
| 80 |
|
| 81 |
a < b; |
| 82 |
a > b; |
| 83 |
|
| 84 |
a <= b; |
| 85 |
a >= b; |
| 86 |
a == b; |
| 87 |
a != b; |
| 88 |
|
| 89 |
a & b; |
| 90 |
a ^ b; |
| 91 |
a | b; |
| 92 |
|
| 93 |
a && b; |
| 94 |
a || b; |
| 95 |
|
| 96 |
a = b; |
| 97 |
|
| 98 |
a *= b; |
| 99 |
a /= b; |
| 100 |
a %= b; |
| 101 |
a += b; |
| 102 |
a -= b; |
| 103 |
|
| 104 |
a <<= b; |
| 105 |
a >>= b; |
| 106 |
|
| 107 |
a &= b; |
| 108 |
a ^= b; |
| 109 |
a |= b; |
| 110 |
a , b; |
| 111 |
|
| 112 |
} |
| 113 |
""" |
| 114 |
|
| 115 |
|
| 116 |
class TestCursor(unittest.TestCase): |
| 117 |
def test_get_children(self): |
| 118 |
tu = get_tu(CHILDREN_TEST) |
| 119 |
|
| 120 |
it = tu.cursor.get_children() |
| 121 |
tu_nodes = list(it) |
| 122 |
|
| 123 |
self.assertEqual(len(tu_nodes), 3) |
| 124 |
for cursor in tu_nodes: |
| 125 |
self.assertIsNotNone(cursor.translation_unit) |
| 126 |
|
| 127 |
self.assertNotEqual(tu_nodes[0], tu_nodes[1]) |
| 128 |
self.assertEqual(tu_nodes[0].kind, CursorKind.STRUCT_DECL) |
| 129 |
self.assertEqual(tu_nodes[0].spelling, "s0") |
| 130 |
self.assertEqual(tu_nodes[0].is_definition(), True) |
| 131 |
self.assertEqual(tu_nodes[0].location.file.name, "t.c") |
| 132 |
self.assertEqual(tu_nodes[0].location.line, 1) |
| 133 |
self.assertEqual(tu_nodes[0].location.column, 8) |
| 134 |
self.assertGreater(tu_nodes[0].hash, 0) |
| 135 |
self.assertIsNotNone(tu_nodes[0].translation_unit) |
| 136 |
|
| 137 |
s0_nodes = list(tu_nodes[0].get_children()) |
| 138 |
self.assertEqual(len(s0_nodes), 2) |
| 139 |
self.assertEqual(s0_nodes[0].kind, CursorKind.FIELD_DECL) |
| 140 |
self.assertEqual(s0_nodes[0].spelling, "a") |
| 141 |
self.assertEqual(s0_nodes[0].type.kind, TypeKind.INT) |
| 142 |
self.assertEqual(s0_nodes[1].kind, CursorKind.FIELD_DECL) |
| 143 |
self.assertEqual(s0_nodes[1].spelling, "b") |
| 144 |
self.assertEqual(s0_nodes[1].type.kind, TypeKind.INT) |
| 145 |
|
| 146 |
self.assertEqual(tu_nodes[1].kind, CursorKind.STRUCT_DECL) |
| 147 |
self.assertEqual(tu_nodes[1].spelling, "s1") |
| 148 |
self.assertEqual(tu_nodes[1].displayname, "s1") |
| 149 |
self.assertEqual(tu_nodes[1].is_definition(), False) |
| 150 |
|
| 151 |
self.assertEqual(tu_nodes[2].kind, CursorKind.FUNCTION_DECL) |
| 152 |
self.assertEqual(tu_nodes[2].spelling, "f0") |
| 153 |
self.assertEqual(tu_nodes[2].displayname, "f0(int, int)") |
| 154 |
self.assertEqual(tu_nodes[2].is_definition(), True) |
| 155 |
|
| 156 |
def test_references(self): |
| 157 |
"""Ensure that references to TranslationUnit are kept.""" |
| 158 |
tu = get_tu("int x;") |
| 159 |
cursors = list(tu.cursor.get_children()) |
| 160 |
self.assertGreater(len(cursors), 0) |
| 161 |
|
| 162 |
cursor = cursors[0] |
| 163 |
self.assertIsInstance(cursor.translation_unit, TranslationUnit) |
| 164 |
|
| 165 |
# Delete reference to TU and perform a full GC. |
| 166 |
del tu |
| 167 |
gc.collect() |
| 168 |
self.assertIsInstance(cursor.translation_unit, TranslationUnit) |
| 169 |
|
| 170 |
# If the TU was destroyed, this should cause a segfault. |
| 171 |
cursor.semantic_parent |
| 172 |
|
| 173 |
def test_canonical(self): |
| 174 |
source = "struct X; struct X; struct X { int member; };" |
| 175 |
tu = get_tu(source) |
| 176 |
|
| 177 |
cursors = [] |
| 178 |
for cursor in tu.cursor.get_children(): |
| 179 |
if cursor.spelling == "X": |
| 180 |
cursors.append(cursor) |
| 181 |
|
| 182 |
self.assertEqual(len(cursors), 3) |
| 183 |
self.assertEqual(cursors[1].canonical, cursors[2].canonical) |
| 184 |
|
| 185 |
def test_is_const_method(self): |
| 186 |
"""Ensure Cursor.is_const_method works.""" |
| 187 |
source = "class X { void foo() const; void bar(); };" |
| 188 |
tu = get_tu(source, lang="cpp") |
| 189 |
|
| 190 |
cls = get_cursor(tu, "X") |
| 191 |
foo = get_cursor(tu, "foo") |
| 192 |
bar = get_cursor(tu, "bar") |
| 193 |
self.assertIsNotNone(cls) |
| 194 |
self.assertIsNotNone(foo) |
| 195 |
self.assertIsNotNone(bar) |
| 196 |
|
| 197 |
self.assertTrue(foo.is_const_method()) |
| 198 |
self.assertFalse(bar.is_const_method()) |
| 199 |
|
| 200 |
def test_is_converting_constructor(self): |
| 201 |
"""Ensure Cursor.is_converting_constructor works.""" |
| 202 |
source = "class X { explicit X(int); X(double); X(); };" |
| 203 |
tu = get_tu(source, lang="cpp") |
| 204 |
|
| 205 |
xs = get_cursors(tu, "X") |
| 206 |
|
| 207 |
self.assertEqual(len(xs), 4) |
| 208 |
self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL) |
| 209 |
cs = xs[1:] |
| 210 |
self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR) |
| 211 |
self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR) |
| 212 |
self.assertEqual(cs[2].kind, CursorKind.CONSTRUCTOR) |
| 213 |
|
| 214 |
self.assertFalse(cs[0].is_converting_constructor()) |
| 215 |
self.assertTrue(cs[1].is_converting_constructor()) |
| 216 |
self.assertFalse(cs[2].is_converting_constructor()) |
| 217 |
|
| 218 |
def test_is_copy_constructor(self): |
| 219 |
"""Ensure Cursor.is_copy_constructor works.""" |
| 220 |
source = "class X { X(); X(const X&); X(X&&); };" |
| 221 |
tu = get_tu(source, lang="cpp") |
| 222 |
|
| 223 |
xs = get_cursors(tu, "X") |
| 224 |
self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL) |
| 225 |
cs = xs[1:] |
| 226 |
self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR) |
| 227 |
self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR) |
| 228 |
self.assertEqual(cs[2].kind, CursorKind.CONSTRUCTOR) |
| 229 |
|
| 230 |
self.assertFalse(cs[0].is_copy_constructor()) |
| 231 |
self.assertTrue(cs[1].is_copy_constructor()) |
| 232 |
self.assertFalse(cs[2].is_copy_constructor()) |
| 233 |
|
| 234 |
def test_is_default_constructor(self): |
| 235 |
"""Ensure Cursor.is_default_constructor works.""" |
| 236 |
source = "class X { X(); X(int); };" |
| 237 |
tu = get_tu(source, lang="cpp") |
| 238 |
|
| 239 |
xs = get_cursors(tu, "X") |
| 240 |
self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL) |
| 241 |
cs = xs[1:] |
| 242 |
self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR) |
| 243 |
self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR) |
| 244 |
|
| 245 |
self.assertTrue(cs[0].is_default_constructor()) |
| 246 |
self.assertFalse(cs[1].is_default_constructor()) |
| 247 |
|
| 248 |
def test_is_move_constructor(self): |
| 249 |
"""Ensure Cursor.is_move_constructor works.""" |
| 250 |
source = "class X { X(); X(const X&); X(X&&); };" |
| 251 |
tu = get_tu(source, lang="cpp") |
| 252 |
|
| 253 |
xs = get_cursors(tu, "X") |
| 254 |
self.assertEqual(xs[0].kind, CursorKind.CLASS_DECL) |
| 255 |
cs = xs[1:] |
| 256 |
self.assertEqual(cs[0].kind, CursorKind.CONSTRUCTOR) |
| 257 |
self.assertEqual(cs[1].kind, CursorKind.CONSTRUCTOR) |
| 258 |
self.assertEqual(cs[2].kind, CursorKind.CONSTRUCTOR) |
| 259 |
|
| 260 |
self.assertFalse(cs[0].is_move_constructor()) |
| 261 |
self.assertFalse(cs[1].is_move_constructor()) |
| 262 |
self.assertTrue(cs[2].is_move_constructor()) |
| 263 |
|
| 264 |
def test_is_default_method(self): |
| 265 |
"""Ensure Cursor.is_default_method works.""" |
| 266 |
source = "class X { X() = default; }; class Y { Y(); };" |
| 267 |
tu = get_tu(source, lang="cpp") |
| 268 |
|
| 269 |
xs = get_cursors(tu, "X") |
| 270 |
ys = get_cursors(tu, "Y") |
| 271 |
|
| 272 |
self.assertEqual(len(xs), 2) |
| 273 |
self.assertEqual(len(ys), 2) |
| 274 |
|
| 275 |
xc = xs[1] |
| 276 |
yc = ys[1] |
| 277 |
|
| 278 |
self.assertTrue(xc.is_default_method()) |
| 279 |
self.assertFalse(yc.is_default_method()) |
| 280 |
|
| 281 |
def test_is_deleted_method(self): |
| 282 |
source = "class X { X() = delete; }; class Y { Y(); };" |
| 283 |
tu = get_tu(source, lang="cpp") |
| 284 |
|
| 285 |
xs = get_cursors(tu, "X") |
| 286 |
ys = get_cursors(tu, "Y") |
| 287 |
|
| 288 |
self.assertEqual(len(xs), 2) |
| 289 |
self.assertEqual(len(ys), 2) |
| 290 |
|
| 291 |
xc = xs[1] |
| 292 |
yc = ys[1] |
| 293 |
|
| 294 |
self.assertTrue(xc.is_deleted_method()) |
| 295 |
self.assertFalse(yc.is_deleted_method()) |
| 296 |
|
| 297 |
def test_is_copy_assignment_operator_method(self): |
| 298 |
source_with_copy_assignment_operators = """ |
| 299 |
struct Foo { |
| 300 |
// Those are copy-assignment operators |
| 301 |
bool operator=(const Foo&); |
| 302 |
bool operator=(Foo&); |
| 303 |
Foo operator=(Foo); |
| 304 |
bool operator=(volatile Foo&); |
| 305 |
bool operator=(const volatile Foo&); |
| 306 |
|
| 307 |
// Positive-check that the recognition works for templated classes too |
| 308 |
template <typename T> |
| 309 |
class Bar { |
| 310 |
bool operator=(const Bar&); |
| 311 |
Bar operator=(const Bar); |
| 312 |
bool operator=(Bar<T>&); |
| 313 |
bool operator=(volatile Bar&); |
| 314 |
bool operator=(const volatile Bar<T>&); |
| 315 |
}; |
| 316 |
""" |
| 317 |
source_without_copy_assignment_operators = """ |
| 318 |
struct Foo { |
| 319 |
// Those are not copy-assignment operators |
| 320 |
template<typename T> |
| 321 |
bool operator=(const T&); |
| 322 |
bool operator=(const bool&); |
| 323 |
bool operator=(char&); |
| 324 |
bool operator=(volatile unsigned int&); |
| 325 |
bool operator=(const volatile unsigned char&); |
| 326 |
bool operator=(int); |
| 327 |
bool operator=(Foo&&); |
| 328 |
}; |
| 329 |
""" |
| 330 |
tu_with_copy_assignment_operators = get_tu( |
| 331 |
source_with_copy_assignment_operators, lang="cpp" |
| 332 |
) |
| 333 |
tu_without_copy_assignment_operators = get_tu( |
| 334 |
source_without_copy_assignment_operators, lang="cpp" |
| 335 |
) |
| 336 |
|
| 337 |
copy_assignment_operators_cursors = get_cursors( |
| 338 |
tu_with_copy_assignment_operators, "operator=" |
| 339 |
) |
| 340 |
non_copy_assignment_operators_cursors = get_cursors( |
| 341 |
tu_without_copy_assignment_operators, "operator=" |
| 342 |
) |
| 343 |
|
| 344 |
self.assertEqual(len(copy_assignment_operators_cursors), 10) |
| 345 |
self.assertEqual(len(non_copy_assignment_operators_cursors), 7) |
| 346 |
|
| 347 |
self.assertTrue( |
| 348 |
all( |
| 349 |
[ |
| 350 |
cursor.is_copy_assignment_operator_method() |
| 351 |
for cursor in copy_assignment_operators_cursors |
| 352 |
] |
| 353 |
) |
| 354 |
) |
| 355 |
|
| 356 |
self.assertFalse( |
| 357 |
any( |
| 358 |
[ |
| 359 |
cursor.is_copy_assignment_operator_method() |
| 360 |
for cursor in non_copy_assignment_operators_cursors |
| 361 |
] |
| 362 |
) |
| 363 |
) |
| 364 |
|
| 365 |
def test_is_move_assignment_operator_method(self): |
| 366 |
"""Ensure Cursor.is_move_assignment_operator_method works.""" |
| 367 |
source_with_move_assignment_operators = """ |
| 368 |
struct Foo { |
| 369 |
// Those are move-assignment operators |
| 370 |
bool operator=(const Foo&&); |
| 371 |
bool operator=(Foo&&); |
| 372 |
bool operator=(volatile Foo&&); |
| 373 |
bool operator=(const volatile Foo&&); |
| 374 |
|
| 375 |
// Positive-check that the recognition works for templated classes too |
| 376 |
template <typename T> |
| 377 |
class Bar { |
| 378 |
bool operator=(const Bar&&); |
| 379 |
bool operator=(Bar<T>&&); |
| 380 |
bool operator=(volatile Bar&&); |
| 381 |
bool operator=(const volatile Bar<T>&&); |
| 382 |
}; |
| 383 |
""" |
| 384 |
source_without_move_assignment_operators = """ |
| 385 |
struct Foo { |
| 386 |
// Those are not move-assignment operators |
| 387 |
template<typename T> |
| 388 |
bool operator=(const T&&); |
| 389 |
bool operator=(const bool&&); |
| 390 |
bool operator=(char&&); |
| 391 |
bool operator=(volatile unsigned int&&); |
| 392 |
bool operator=(const volatile unsigned char&&); |
| 393 |
bool operator=(int); |
| 394 |
bool operator=(Foo); |
| 395 |
}; |
| 396 |
""" |
| 397 |
tu_with_move_assignment_operators = get_tu( |
| 398 |
source_with_move_assignment_operators, lang="cpp" |
| 399 |
) |
| 400 |
tu_without_move_assignment_operators = get_tu( |
| 401 |
source_without_move_assignment_operators, lang="cpp" |
| 402 |
) |
| 403 |
|
| 404 |
move_assignment_operators_cursors = get_cursors( |
| 405 |
tu_with_move_assignment_operators, "operator=" |
| 406 |
) |
| 407 |
non_move_assignment_operators_cursors = get_cursors( |
| 408 |
tu_without_move_assignment_operators, "operator=" |
| 409 |
) |
| 410 |
|
| 411 |
self.assertEqual(len(move_assignment_operators_cursors), 8) |
| 412 |
self.assertTrue(len(non_move_assignment_operators_cursors), 7) |
| 413 |
|
| 414 |
self.assertTrue( |
| 415 |
all( |
| 416 |
[ |
| 417 |
cursor.is_move_assignment_operator_method() |
| 418 |
for cursor in move_assignment_operators_cursors |
| 419 |
] |
| 420 |
) |
| 421 |
) |
| 422 |
self.assertFalse( |
| 423 |
any( |
| 424 |
[ |
| 425 |
cursor.is_move_assignment_operator_method() |
| 426 |
for cursor in non_move_assignment_operators_cursors |
| 427 |
] |
| 428 |
) |
| 429 |
) |
| 430 |
|
| 431 |
def test_is_explicit_method(self): |
| 432 |
"""Ensure Cursor.is_explicit_method works.""" |
| 433 |
source_with_explicit_methods = """ |
| 434 |
struct Foo { |
| 435 |
// Those are explicit |
| 436 |
explicit Foo(double); |
| 437 |
explicit(true) Foo(char); |
| 438 |
explicit operator double(); |
| 439 |
explicit(true) operator char(); |
| 440 |
}; |
| 441 |
""" |
| 442 |
source_without_explicit_methods = """ |
| 443 |
struct Foo { |
| 444 |
// Those are not explicit |
| 445 |
Foo(int); |
| 446 |
explicit(false) Foo(float); |
| 447 |
operator int(); |
| 448 |
explicit(false) operator float(); |
| 449 |
}; |
| 450 |
""" |
| 451 |
tu_with_explicit_methods = get_tu(source_with_explicit_methods, lang="cpp") |
| 452 |
tu_without_explicit_methods = get_tu( |
| 453 |
source_without_explicit_methods, lang="cpp" |
| 454 |
) |
| 455 |
|
| 456 |
explicit_methods_cursors = [ |
| 457 |
*get_cursors(tu_with_explicit_methods, "Foo")[1:], |
| 458 |
get_cursor(tu_with_explicit_methods, "operator double"), |
| 459 |
get_cursor(tu_with_explicit_methods, "operator char"), |
| 460 |
] |
| 461 |
|
| 462 |
non_explicit_methods_cursors = [ |
| 463 |
*get_cursors(tu_without_explicit_methods, "Foo")[1:], |
| 464 |
get_cursor(tu_without_explicit_methods, "operator int"), |
| 465 |
get_cursor(tu_without_explicit_methods, "operator float"), |
| 466 |
] |
| 467 |
|
| 468 |
self.assertEqual(len(explicit_methods_cursors), 4) |
| 469 |
self.assertTrue(len(non_explicit_methods_cursors), 4) |
| 470 |
|
| 471 |
self.assertTrue( |
| 472 |
all([cursor.is_explicit_method() for cursor in explicit_methods_cursors]) |
| 473 |
) |
| 474 |
self.assertFalse( |
| 475 |
any( |
| 476 |
[cursor.is_explicit_method() for cursor in non_explicit_methods_cursors] |
| 477 |
) |
| 478 |
) |
| 479 |
|
| 480 |
def test_is_mutable_field(self): |
| 481 |
"""Ensure Cursor.is_mutable_field works.""" |
| 482 |
source = "class X { int x_; mutable int y_; };" |
| 483 |
tu = get_tu(source, lang="cpp") |
| 484 |
|
| 485 |
cls = get_cursor(tu, "X") |
| 486 |
x_ = get_cursor(tu, "x_") |
| 487 |
y_ = get_cursor(tu, "y_") |
| 488 |
self.assertIsNotNone(cls) |
| 489 |
self.assertIsNotNone(x_) |
| 490 |
self.assertIsNotNone(y_) |
| 491 |
|
| 492 |
self.assertFalse(x_.is_mutable_field()) |
| 493 |
self.assertTrue(y_.is_mutable_field()) |
| 494 |
|
| 495 |
def test_is_static_method(self): |
| 496 |
"""Ensure Cursor.is_static_method works.""" |
| 497 |
|
| 498 |
source = "class X { static void foo(); void bar(); };" |
| 499 |
tu = get_tu(source, lang="cpp") |
| 500 |
|
| 501 |
cls = get_cursor(tu, "X") |
| 502 |
foo = get_cursor(tu, "foo") |
| 503 |
bar = get_cursor(tu, "bar") |
| 504 |
self.assertIsNotNone(cls) |
| 505 |
self.assertIsNotNone(foo) |
| 506 |
self.assertIsNotNone(bar) |
| 507 |
|
| 508 |
self.assertTrue(foo.is_static_method()) |
| 509 |
self.assertFalse(bar.is_static_method()) |
| 510 |
|
| 511 |
def test_is_pure_virtual_method(self): |
| 512 |
"""Ensure Cursor.is_pure_virtual_method works.""" |
| 513 |
source = "class X { virtual void foo() = 0; virtual void bar(); };" |
| 514 |
tu = get_tu(source, lang="cpp") |
| 515 |
|
| 516 |
cls = get_cursor(tu, "X") |
| 517 |
foo = get_cursor(tu, "foo") |
| 518 |
bar = get_cursor(tu, "bar") |
| 519 |
self.assertIsNotNone(cls) |
| 520 |
self.assertIsNotNone(foo) |
| 521 |
self.assertIsNotNone(bar) |
| 522 |
|
| 523 |
self.assertTrue(foo.is_pure_virtual_method()) |
| 524 |
self.assertFalse(bar.is_pure_virtual_method()) |
| 525 |
|
| 526 |
def test_is_virtual_method(self): |
| 527 |
"""Ensure Cursor.is_virtual_method works.""" |
| 528 |
source = "class X { virtual void foo(); void bar(); };" |
| 529 |
tu = get_tu(source, lang="cpp") |
| 530 |
|
| 531 |
cls = get_cursor(tu, "X") |
| 532 |
foo = get_cursor(tu, "foo") |
| 533 |
bar = get_cursor(tu, "bar") |
| 534 |
self.assertIsNotNone(cls) |
| 535 |
self.assertIsNotNone(foo) |
| 536 |
self.assertIsNotNone(bar) |
| 537 |
|
| 538 |
self.assertTrue(foo.is_virtual_method()) |
| 539 |
self.assertFalse(bar.is_virtual_method()) |
| 540 |
|
| 541 |
def test_is_abstract_record(self): |
| 542 |
"""Ensure Cursor.is_abstract_record works.""" |
| 543 |
source = "struct X { virtual void x() = 0; }; struct Y : X { void x(); };" |
| 544 |
tu = get_tu(source, lang="cpp") |
| 545 |
|
| 546 |
cls = get_cursor(tu, "X") |
| 547 |
self.assertTrue(cls.is_abstract_record()) |
| 548 |
|
| 549 |
cls = get_cursor(tu, "Y") |
| 550 |
self.assertFalse(cls.is_abstract_record()) |
| 551 |
|
| 552 |
def test_is_scoped_enum(self): |
| 553 |
"""Ensure Cursor.is_scoped_enum works.""" |
| 554 |
source = "class X {}; enum RegularEnum {}; enum class ScopedEnum {};" |
| 555 |
tu = get_tu(source, lang="cpp") |
| 556 |
|
| 557 |
cls = get_cursor(tu, "X") |
| 558 |
regular_enum = get_cursor(tu, "RegularEnum") |
| 559 |
scoped_enum = get_cursor(tu, "ScopedEnum") |
| 560 |
self.assertIsNotNone(cls) |
| 561 |
self.assertIsNotNone(regular_enum) |
| 562 |
self.assertIsNotNone(scoped_enum) |
| 563 |
|
| 564 |
self.assertFalse(cls.is_scoped_enum()) |
| 565 |
self.assertFalse(regular_enum.is_scoped_enum()) |
| 566 |
self.assertTrue(scoped_enum.is_scoped_enum()) |
| 567 |
|
| 568 |
def test_get_definition(self): |
| 569 |
"""Ensure Cursor.get_definition works.""" |
| 570 |
tu = get_tu( |
| 571 |
""" |
| 572 |
class A { |
| 573 |
constexpr static int f(){return 3;} |
| 574 |
}; |
| 575 |
struct B { |
| 576 |
int b = A::f(); |
| 577 |
}; |
| 578 |
""", |
| 579 |
lang="cpp", |
| 580 |
) |
| 581 |
curs = get_cursors(tu, "f") |
| 582 |
self.assertEqual(len(curs), 4) |
| 583 |
self.assertEqual(curs[0].kind, CursorKind.CXX_METHOD) |
| 584 |
self.assertEqual(curs[1].get_definition(), curs[0]) |
| 585 |
self.assertEqual(curs[2].get_definition(), curs[0]) |
| 586 |
self.assertEqual(curs[3].get_definition(), curs[0]) |
| 587 |
|
| 588 |
def test_get_usr(self): |
| 589 |
"""Ensure Cursor.get_usr works.""" |
| 590 |
tu = get_tu( |
| 591 |
""" |
| 592 |
int add(int, int); |
| 593 |
int add(int a, int b) { return a + b; } |
| 594 |
int add(float a, float b) { return a + b; } |
| 595 |
""", |
| 596 |
lang="cpp", |
| 597 |
) |
| 598 |
curs = get_cursors(tu, "add") |
| 599 |
self.assertEqual(len(curs), 3) |
| 600 |
self.assertEqual(curs[0].get_usr(), curs[1].get_usr()) |
| 601 |
self.assertNotEqual(curs[0].get_usr(), curs[2].get_usr()) |
| 602 |
|
| 603 |
def test_underlying_type(self): |
| 604 |
tu = get_tu("typedef int foo;") |
| 605 |
typedef = get_cursor(tu, "foo") |
| 606 |
self.assertIsNotNone(typedef) |
| 607 |
|
| 608 |
self.assertTrue(typedef.kind.is_declaration()) |
| 609 |
underlying = typedef.underlying_typedef_type |
| 610 |
self.assertEqual(underlying.kind, TypeKind.INT) |
| 611 |
|
| 612 |
def test_semantic_parent(self): |
| 613 |
tu = get_tu(PARENT_TEST, "cpp") |
| 614 |
curs = get_cursors(tu, "f") |
| 615 |
decl = get_cursor(tu, "C") |
| 616 |
self.assertEqual(len(curs), 2) |
| 617 |
self.assertEqual(curs[0].semantic_parent, curs[1].semantic_parent) |
| 618 |
self.assertEqual(curs[0].semantic_parent, decl) |
| 619 |
|
| 620 |
def test_lexical_parent(self): |
| 621 |
tu = get_tu(PARENT_TEST, "cpp") |
| 622 |
curs = get_cursors(tu, "f") |
| 623 |
decl = get_cursor(tu, "C") |
| 624 |
self.assertEqual(len(curs), 2) |
| 625 |
self.assertNotEqual(curs[0].lexical_parent, curs[1].lexical_parent) |
| 626 |
self.assertEqual(curs[0].lexical_parent, decl) |
| 627 |
self.assertEqual(curs[1].lexical_parent, tu.cursor) |
| 628 |
|
| 629 |
def test_enum_type(self): |
| 630 |
tu = get_tu("enum TEST { FOO=1, BAR=2 };") |
| 631 |
enum = get_cursor(tu, "TEST") |
| 632 |
self.assertIsNotNone(enum) |
| 633 |
|
| 634 |
self.assertEqual(enum.kind, CursorKind.ENUM_DECL) |
| 635 |
enum_type = enum.enum_type |
| 636 |
self.assertIn(enum_type.kind, (TypeKind.UINT, TypeKind.INT)) |
| 637 |
|
| 638 |
def test_enum_type_cpp(self): |
| 639 |
tu = get_tu("enum TEST : long long { FOO=1, BAR=2 };", lang="cpp") |
| 640 |
enum = get_cursor(tu, "TEST") |
| 641 |
self.assertIsNotNone(enum) |
| 642 |
|
| 643 |
self.assertEqual(enum.kind, CursorKind.ENUM_DECL) |
| 644 |
self.assertEqual(enum.enum_type.kind, TypeKind.LONGLONG) |
| 645 |
|
| 646 |
def test_objc_type_encoding(self): |
| 647 |
tu = get_tu("int i;", lang="objc") |
| 648 |
i = get_cursor(tu, "i") |
| 649 |
|
| 650 |
self.assertIsNotNone(i) |
| 651 |
self.assertEqual(i.objc_type_encoding, "i") |
| 652 |
|
| 653 |
def test_enum_values(self): |
| 654 |
tu = get_tu("enum TEST { SPAM=1, EGG, HAM = EGG * 20};") |
| 655 |
enum = get_cursor(tu, "TEST") |
| 656 |
self.assertIsNotNone(enum) |
| 657 |
|
| 658 |
self.assertEqual(enum.kind, CursorKind.ENUM_DECL) |
| 659 |
|
| 660 |
enum_constants = list(enum.get_children()) |
| 661 |
self.assertEqual(len(enum_constants), 3) |
| 662 |
|
| 663 |
spam, egg, ham = enum_constants |
| 664 |
|
| 665 |
self.assertEqual(spam.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 666 |
self.assertEqual(spam.enum_value, 1) |
| 667 |
self.assertEqual(egg.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 668 |
self.assertEqual(egg.enum_value, 2) |
| 669 |
self.assertEqual(ham.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 670 |
self.assertEqual(ham.enum_value, 40) |
| 671 |
|
| 672 |
def test_enum_values_cpp(self): |
| 673 |
tu = get_tu( |
| 674 |
"enum TEST : long long { SPAM = -1, HAM = 0x10000000000};", lang="cpp" |
| 675 |
) |
| 676 |
enum = get_cursor(tu, "TEST") |
| 677 |
self.assertIsNotNone(enum) |
| 678 |
|
| 679 |
self.assertEqual(enum.kind, CursorKind.ENUM_DECL) |
| 680 |
|
| 681 |
enum_constants = list(enum.get_children()) |
| 682 |
self.assertEqual(len(enum_constants), 2) |
| 683 |
|
| 684 |
spam, ham = enum_constants |
| 685 |
|
| 686 |
self.assertEqual(spam.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 687 |
self.assertEqual(spam.enum_value, -1) |
| 688 |
self.assertEqual(ham.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 689 |
self.assertEqual(ham.enum_value, 0x10000000000) |
| 690 |
|
| 691 |
def test_enum_values_unsigned(self): |
| 692 |
tu = get_tu("enum TEST : unsigned char { SPAM=0, HAM = 200};", lang="cpp") |
| 693 |
enum = get_cursor(tu, "TEST") |
| 694 |
self.assertIsNotNone(enum) |
| 695 |
|
| 696 |
self.assertEqual(enum.kind, CursorKind.ENUM_DECL) |
| 697 |
|
| 698 |
enum_constants = list(enum.get_children()) |
| 699 |
self.assertEqual(len(enum_constants), 2) |
| 700 |
|
| 701 |
spam, ham = enum_constants |
| 702 |
|
| 703 |
self.assertEqual(spam.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 704 |
self.assertEqual(spam.enum_value, 0) |
| 705 |
self.assertEqual(ham.kind, CursorKind.ENUM_CONSTANT_DECL) |
| 706 |
self.assertEqual(ham.enum_value, 200) |
| 707 |
|
| 708 |
def test_annotation_attribute(self): |
| 709 |
tu = get_tu( |
| 710 |
'int foo (void) __attribute__ ((annotate("here be annotation attribute")));' |
| 711 |
) |
| 712 |
|
| 713 |
foo = get_cursor(tu, "foo") |
| 714 |
self.assertIsNotNone(foo) |
| 715 |
|
| 716 |
for c in foo.get_children(): |
| 717 |
if c.kind == CursorKind.ANNOTATE_ATTR: |
| 718 |
self.assertEqual(c.displayname, "here be annotation attribute") |
| 719 |
break |
| 720 |
else: |
| 721 |
self.fail("Couldn't find annotation") |
| 722 |
|
| 723 |
def test_annotation_template(self): |
| 724 |
annotation = '__attribute__ ((annotate("annotation")))' |
| 725 |
for source, kind in [ |
| 726 |
("int foo (T value) %s;", CursorKind.FUNCTION_TEMPLATE), |
| 727 |
("class %s foo {};", CursorKind.CLASS_TEMPLATE), |
| 728 |
]: |
| 729 |
source = "template<typename T> " + (source % annotation) |
| 730 |
tu = get_tu(source, lang="cpp") |
| 731 |
|
| 732 |
foo = get_cursor(tu, "foo") |
| 733 |
self.assertIsNotNone(foo) |
| 734 |
self.assertEqual(foo.kind, kind) |
| 735 |
|
| 736 |
for c in foo.get_children(): |
| 737 |
if c.kind == CursorKind.ANNOTATE_ATTR: |
| 738 |
self.assertEqual(c.displayname, "annotation") |
| 739 |
break |
| 740 |
else: |
| 741 |
self.fail("Couldn't find annotation for {}".format(kind)) |
| 742 |
|
| 743 |
def test_result_type(self): |
| 744 |
tu = get_tu("int foo();") |
| 745 |
foo = get_cursor(tu, "foo") |
| 746 |
|
| 747 |
self.assertIsNotNone(foo) |
| 748 |
t = foo.result_type |
| 749 |
self.assertEqual(t.kind, TypeKind.INT) |
| 750 |
|
| 751 |
def test_result_type_objc_method_decl(self): |
| 752 |
code = """\ |
| 753 |
@interface Interface : NSObject |
| 754 |
-(void)voidMethod; |
| 755 |
@end |
| 756 |
""" |
| 757 |
tu = get_tu(code, lang="objc") |
| 758 |
cursor = get_cursor(tu, "voidMethod") |
| 759 |
result_type = cursor.result_type |
| 760 |
self.assertEqual(cursor.kind, CursorKind.OBJC_INSTANCE_METHOD_DECL) |
| 761 |
self.assertEqual(result_type.kind, TypeKind.VOID) |
| 762 |
|
| 763 |
def test_storage_class(self): |
| 764 |
tu = get_tu( |
| 765 |
""" |
| 766 |
extern int ex; |
| 767 |
register int reg; |
| 768 |
int count(int a, int b){ |
| 769 |
static int counter = 0; |
| 770 |
return 0; |
| 771 |
} |
| 772 |
""", |
| 773 |
lang="cpp", |
| 774 |
) |
| 775 |
cursor = get_cursor(tu, "ex") |
| 776 |
self.assertEqual(cursor.storage_class, StorageClass.EXTERN) |
| 777 |
cursor = get_cursor(tu, "counter") |
| 778 |
self.assertEqual(cursor.storage_class, StorageClass.STATIC) |
| 779 |
cursor = get_cursor(tu, "reg") |
| 780 |
self.assertEqual(cursor.storage_class, StorageClass.REGISTER) |
| 781 |
|
| 782 |
def test_function_inlined(self): |
| 783 |
tu = get_tu( |
| 784 |
""" |
| 785 |
inline void f_inline(void); |
| 786 |
void f_noninline(void); |
| 787 |
int d_noninline; |
| 788 |
""" |
| 789 |
) |
| 790 |
cursor = get_cursor(tu, "f_inline") |
| 791 |
self.assertEqual(cursor.is_function_inlined(), True) |
| 792 |
cursor = get_cursor(tu, "f_noninline") |
| 793 |
self.assertEqual(cursor.is_function_inlined(), False) |
| 794 |
cursor = get_cursor(tu, "d_noninline") |
| 795 |
self.assertEqual(cursor.is_function_inlined(), False) |
| 796 |
|
| 797 |
def test_availability(self): |
| 798 |
tu = get_tu("class A { A(A const&) = delete; };", lang="cpp") |
| 799 |
|
| 800 |
# AvailabilityKind.AVAILABLE |
| 801 |
cursor = get_cursor(tu, "A") |
| 802 |
self.assertEqual(cursor.kind, CursorKind.CLASS_DECL) |
| 803 |
self.assertEqual(cursor.availability, AvailabilityKind.AVAILABLE) |
| 804 |
|
| 805 |
# AvailabilityKind.NOT_AVAILABLE |
| 806 |
cursors = get_cursors(tu, "A") |
| 807 |
for c in cursors: |
| 808 |
if c.kind == CursorKind.CONSTRUCTOR: |
| 809 |
self.assertEqual(c.availability, AvailabilityKind.NOT_AVAILABLE) |
| 810 |
break |
| 811 |
else: |
| 812 |
self.fail("Could not find cursor for deleted constructor") |
| 813 |
|
| 814 |
# AvailabilityKind.DEPRECATED |
| 815 |
tu = get_tu("void test() __attribute__((deprecated));", lang="cpp") |
| 816 |
cursor = get_cursor(tu, "test") |
| 817 |
self.assertEqual(cursor.availability, AvailabilityKind.DEPRECATED) |
| 818 |
|
| 819 |
# AvailabilityKind.NOT_ACCESSIBLE is only used in the code completion results |
| 820 |
|
| 821 |
def test_get_tokens(self): |
| 822 |
"""Ensure we can map cursors back to tokens.""" |
| 823 |
tu = get_tu("int foo(int i);") |
| 824 |
foo = get_cursor(tu, "foo") |
| 825 |
|
| 826 |
tokens = list(foo.get_tokens()) |
| 827 |
self.assertEqual(len(tokens), 6) |
| 828 |
self.assertEqual(tokens[0].spelling, "int") |
| 829 |
self.assertEqual(tokens[1].spelling, "foo") |
| 830 |
|
| 831 |
def test_get_token_cursor(self): |
| 832 |
"""Ensure we can map tokens to cursors.""" |
| 833 |
tu = get_tu("class A {}; int foo(A var = A());", lang="cpp") |
| 834 |
foo = get_cursor(tu, "foo") |
| 835 |
|
| 836 |
for cursor in foo.walk_preorder(): |
| 837 |
if cursor.kind.is_expression() and not cursor.kind.is_statement(): |
| 838 |
break |
| 839 |
else: |
| 840 |
self.fail("Could not find default value expression") |
| 841 |
|
| 842 |
tokens = list(cursor.get_tokens()) |
| 843 |
self.assertEqual(len(tokens), 4, [t.spelling for t in tokens]) |
| 844 |
self.assertEqual(tokens[0].spelling, "=") |
| 845 |
self.assertEqual(tokens[1].spelling, "A") |
| 846 |
self.assertEqual(tokens[2].spelling, "(") |
| 847 |
self.assertEqual(tokens[3].spelling, ")") |
| 848 |
t_cursor = tokens[1].cursor |
| 849 |
self.assertEqual(t_cursor.kind, CursorKind.TYPE_REF) |
| 850 |
r_cursor = t_cursor.referenced # should not raise an exception |
| 851 |
self.assertEqual(r_cursor.kind, CursorKind.CLASS_DECL) |
| 852 |
|
| 853 |
def test_get_field_offsetof(self): |
| 854 |
tu = get_tu( |
| 855 |
"struct myStruct {int a; char b; char c; short d; char e;};", lang="cpp" |
| 856 |
) |
| 857 |
c1 = get_cursor(tu, "myStruct") |
| 858 |
c2 = get_cursor(tu, "a") |
| 859 |
c3 = get_cursor(tu, "b") |
| 860 |
c4 = get_cursor(tu, "c") |
| 861 |
c5 = get_cursor(tu, "d") |
| 862 |
c6 = get_cursor(tu, "e") |
| 863 |
self.assertEqual(c1.get_field_offsetof(), -1) |
| 864 |
self.assertEqual(c2.get_field_offsetof(), 0) |
| 865 |
self.assertEqual(c3.get_field_offsetof(), 32) |
| 866 |
self.assertEqual(c4.get_field_offsetof(), 40) |
| 867 |
self.assertEqual(c5.get_field_offsetof(), 48) |
| 868 |
self.assertEqual(c6.get_field_offsetof(), 64) |
| 869 |
|
| 870 |
def test_get_arguments(self): |
| 871 |
tu = get_tu("void foo(int i, int j);") |
| 872 |
foo = get_cursor(tu, "foo") |
| 873 |
arguments = list(foo.get_arguments()) |
| 874 |
|
| 875 |
self.assertEqual(len(arguments), 2) |
| 876 |
self.assertEqual(arguments[0].spelling, "i") |
| 877 |
self.assertEqual(arguments[1].spelling, "j") |
| 878 |
|
| 879 |
def test_get_num_template_arguments(self): |
| 880 |
tu = get_tu(TEMPLATE_ARG_TEST, lang="cpp") |
| 881 |
foos = get_cursors(tu, "foo") |
| 882 |
|
| 883 |
self.assertEqual(foos[1].get_num_template_arguments(), 3) |
| 884 |
|
| 885 |
def test_get_template_argument_kind(self): |
| 886 |
tu = get_tu(TEMPLATE_ARG_TEST, lang="cpp") |
| 887 |
foos = get_cursors(tu, "foo") |
| 888 |
|
| 889 |
self.assertEqual( |
| 890 |
foos[1].get_template_argument_kind(0), TemplateArgumentKind.INTEGRAL |
| 891 |
) |
| 892 |
self.assertEqual( |
| 893 |
foos[1].get_template_argument_kind(1), TemplateArgumentKind.TYPE |
| 894 |
) |
| 895 |
self.assertEqual( |
| 896 |
foos[1].get_template_argument_kind(2), TemplateArgumentKind.INTEGRAL |
| 897 |
) |
| 898 |
|
| 899 |
def test_get_template_argument_type(self): |
| 900 |
tu = get_tu(TEMPLATE_ARG_TEST, lang="cpp") |
| 901 |
foos = get_cursors(tu, "foo") |
| 902 |
|
| 903 |
self.assertEqual(foos[1].get_template_argument_type(1).kind, TypeKind.FLOAT) |
| 904 |
|
| 905 |
def test_get_template_argument_value(self): |
| 906 |
tu = get_tu(TEMPLATE_ARG_TEST, lang="cpp") |
| 907 |
foos = get_cursors(tu, "foo") |
| 908 |
|
| 909 |
self.assertEqual(foos[1].get_template_argument_value(0), -7) |
| 910 |
self.assertEqual(foos[1].get_template_argument_value(2), True) |
| 911 |
|
| 912 |
def test_get_template_argument_unsigned_value(self): |
| 913 |
tu = get_tu(TEMPLATE_ARG_TEST, lang="cpp") |
| 914 |
foos = get_cursors(tu, "foo") |
| 915 |
|
| 916 |
self.assertEqual(foos[1].get_template_argument_unsigned_value(0), 2**32 - 7) |
| 917 |
self.assertEqual(foos[1].get_template_argument_unsigned_value(2), True) |
| 918 |
|
| 919 |
def test_referenced(self): |
| 920 |
tu = get_tu("void foo(); void bar() { foo(); }") |
| 921 |
foo = get_cursor(tu, "foo") |
| 922 |
bar = get_cursor(tu, "bar") |
| 923 |
for c in bar.get_children(): |
| 924 |
if c.kind == CursorKind.CALL_EXPR: |
| 925 |
self.assertEqual(c.referenced.spelling, foo.spelling) |
| 926 |
break |
| 927 |
|
| 928 |
def test_mangled_name(self): |
| 929 |
kInputForMangling = """\ |
| 930 |
int foo(int, int); |
| 931 |
""" |
| 932 |
tu = get_tu(kInputForMangling, lang="cpp") |
| 933 |
foo = get_cursor(tu, "foo") |
| 934 |
|
| 935 |
# Since libclang does not link in targets, we cannot pass a triple to it |
| 936 |
# and force the target. To enable this test to pass on all platforms, accept |
| 937 |
# all valid manglings. |
| 938 |
# [c-index-test handles this by running the source through clang, emitting |
| 939 |
# an AST file and running libclang on that AST file] |
| 940 |
self.assertIn( |
| 941 |
foo.mangled_name, ("_Z3fooii", "__Z3fooii", "?foo@@YAHHH", "?foo@@YAHHH@Z") |
| 942 |
) |
| 943 |
|
| 944 |
def test_binop(self): |
| 945 |
tu = get_tu(BINOPS, lang="cpp") |
| 946 |
|
| 947 |
operators = { |
| 948 |
# not exposed yet |
| 949 |
# ".*" : BinaryOperator.PtrMemD, |
| 950 |
"->*": BinaryOperator.PtrMemI, |
| 951 |
"*": BinaryOperator.Mul, |
| 952 |
"/": BinaryOperator.Div, |
| 953 |
"%": BinaryOperator.Rem, |
| 954 |
"+": BinaryOperator.Add, |
| 955 |
"-": BinaryOperator.Sub, |
| 956 |
"<<": BinaryOperator.Shl, |
| 957 |
">>": BinaryOperator.Shr, |
| 958 |
# tests do not run in C++2a mode so this operator is not available |
| 959 |
# "<=>" : BinaryOperator.Cmp, |
| 960 |
"<": BinaryOperator.LT, |
| 961 |
">": BinaryOperator.GT, |
| 962 |
"<=": BinaryOperator.LE, |
| 963 |
">=": BinaryOperator.GE, |
| 964 |
"==": BinaryOperator.EQ, |
| 965 |
"!=": BinaryOperator.NE, |
| 966 |
"&": BinaryOperator.And, |
| 967 |
"^": BinaryOperator.Xor, |
| 968 |
"|": BinaryOperator.Or, |
| 969 |
"&&": BinaryOperator.LAnd, |
| 970 |
"||": BinaryOperator.LOr, |
| 971 |
"=": BinaryOperator.Assign, |
| 972 |
"*=": BinaryOperator.MulAssign, |
| 973 |
"/=": BinaryOperator.DivAssign, |
| 974 |
"%=": BinaryOperator.RemAssign, |
| 975 |
"+=": BinaryOperator.AddAssign, |
| 976 |
"-=": BinaryOperator.SubAssign, |
| 977 |
"<<=": BinaryOperator.ShlAssign, |
| 978 |
">>=": BinaryOperator.ShrAssign, |
| 979 |
"&=": BinaryOperator.AndAssign, |
| 980 |
"^=": BinaryOperator.XorAssign, |
| 981 |
"|=": BinaryOperator.OrAssign, |
| 982 |
",": BinaryOperator.Comma, |
| 983 |
} |
| 984 |
|
| 985 |
for op, typ in operators.items(): |
| 986 |
c = get_cursor(tu, op) |
| 987 |
assert c.binary_operator == typ |
| 988 |
|
| 989 |
def test_from_result_null(self): |
| 990 |
tu = get_tu("int a = 1+2;", lang="cpp") |
| 991 |
op = next(next(tu.cursor.get_children()).get_children()) |
| 992 |
self.assertEqual(op.kind, CursorKind.BINARY_OPERATOR) |
| 993 |
self.assertEqual(op.get_definition(), None) |
| 994 |
|
| 995 |
def test_from_cursor_result_null(self): |
| 996 |
tu = get_tu("") |
| 997 |
self.assertEqual(tu.cursor.semantic_parent, None) |
| 998 |
|
| 999 |
def test_pretty_print(self): |
| 1000 |
tu = get_tu("struct X { int x; }; void f(bool x) { }", lang="cpp") |
| 1001 |
f = get_cursor(tu, "f") |
| 1002 |
|
| 1003 |
self.assertEqual(f.displayname, "f(bool)") |
| 1004 |
pp = PrintingPolicy.create(f) |
| 1005 |
self.assertEqual(pp.get_property(PrintingPolicyProperty.Bool), True) |
| 1006 |
self.assertEqual(f.pretty_printed(pp), "void f(bool x) {\n}\n") |
| 1007 |
pp.set_property(PrintingPolicyProperty.Bool, False) |
| 1008 |
self.assertEqual(pp.get_property(PrintingPolicyProperty.Bool), False) |
| 1009 |
self.assertEqual(f.pretty_printed(pp), "void f(_Bool x) {\n}\n") |
| 1010 |
|
| 1011 |
def test_hash(self): |
| 1012 |
def accumulate_cursors(cursor: Cursor, all_cursors: list): |
| 1013 |
all_cursors.append(cursor) |
| 1014 |
for child in cursor.get_children(): |
| 1015 |
all_cursors = accumulate_cursors(child, all_cursors) |
| 1016 |
return all_cursors |
| 1017 |
|
| 1018 |
tu = get_tu(CHILDREN_TEST) |
| 1019 |
all_cursors = accumulate_cursors(tu.cursor, []) |
| 1020 |
cursor_hashes = set() |
| 1021 |
for cursor in all_cursors: |
| 1022 |
self.assertNotIn(hash(cursor), cursor_hashes) |
| 1023 |
cursor_hashes.add(hash(cursor)) |
| 1024 |
|
| 1025 |
def test_has_attrs(self): |
| 1026 |
tu = get_tu( |
| 1027 |
""" |
| 1028 |
struct A; |
| 1029 |
struct A final {}; |
| 1030 |
|
| 1031 |
struct B; |
| 1032 |
struct B {}; |
| 1033 |
""", |
| 1034 |
lang="cpp", |
| 1035 |
) |
| 1036 |
A = get_cursor(tu, "A") |
| 1037 |
B = get_cursor(tu, "B") |
| 1038 |
self.assertTrue(A.get_definition().has_attrs()) |
| 1039 |
self.assertFalse(B.get_definition().has_attrs()) |
| 1040 |
|
| 1041 |
def test_specialized_template(self): |
| 1042 |
tu = get_tu(TEMPLATE_ARG_TEST, lang="cpp") |
| 1043 |
foos = get_cursors(tu, "foo") |
| 1044 |
prime_foo = foos[1].specialized_template |
| 1045 |
|
| 1046 |
self.assertNotEqual(foos[0], foos[1]) |
| 1047 |
self.assertEqual(foos[0], prime_foo) |
| 1048 |
self.assertIsNone(tu.cursor.specialized_template) |
| 1049 |
|
| 1050 |
def test_equality(self): |
| 1051 |
tu = get_tu(CHILDREN_TEST, lang="cpp") |
| 1052 |
cursor1 = get_cursor(tu, "s0") |
| 1053 |
cursor1_2 = get_cursor(tu, "s0") |
| 1054 |
cursor2 = get_cursor(tu, "f0") |
| 1055 |
|
| 1056 |
self.assertIsNotNone(cursor1) |
| 1057 |
self.assertIsNotNone(cursor1_2) |
| 1058 |
self.assertIsNotNone(cursor2) |
| 1059 |
|
| 1060 |
self.assertEqual(cursor1, cursor1) |
| 1061 |
self.assertEqual(cursor1, cursor1_2) |
| 1062 |
self.assertNotEqual(cursor1, cursor2) |
| 1063 |
self.assertNotEqual(cursor1, "foo") |
| 1064 |
|
| 1065 |
def test_null_cursor(self): |
| 1066 |
tu = get_tu("int a = 729;") |
| 1067 |
|
| 1068 |
for cursor in tu.cursor.walk_preorder(): |
| 1069 |
self.assertFalse(cursor.is_null()) |
| 1070 |
|
| 1071 |
nc = conf.lib.clang_getNullCursor() |
| 1072 |
self.assertTrue(nc.is_null()) |
| 1073 |
with self.assertRaises(Exception): |
| 1074 |
nc.is_definition() |
| 1075 |
with self.assertRaises(Exception): |
| 1076 |
nc.spelling |