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