|
13 | 13 | from clang.cindex import TemplateArgumentKind
|
14 | 14 | from clang.cindex import TranslationUnit
|
15 | 15 | from clang.cindex import TypeKind
|
| 16 | +from clang.cindex import BinaryOperator |
16 | 17 | from .util import get_cursor
|
17 | 18 | from .util import get_cursors
|
18 | 19 | from .util import get_tu
|
@@ -54,6 +55,64 @@ class C {
|
54 | 55 | void foo<-7, float, true>();
|
55 | 56 | """
|
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 | + |
57 | 116 |
|
58 | 117 | class TestCursor(unittest.TestCase):
|
59 | 118 | def test_get_children(self):
|
@@ -695,3 +754,48 @@ def test_mangled_name(self):
|
695 | 754 | self.assertIn(
|
696 | 755 | foo.mangled_name, ("_Z3fooii", "__Z3fooii", "?foo@@YAHHH", "?foo@@YAHHH@Z")
|
697 | 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 |
0 commit comments