[clang] Retrieve BinaryOperator::getOpcode and BinaryOperator::getOpcodeStr via libclang and its python interface (PR #98489)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 11 08:13:15 PDT 2024


================
@@ -2181,6 +2193,60 @@ def from_cursor_result(res, fn, args):
         res._tu = args[0]._tu
         return res
 
+class BinaryOperator(BaseEnumeration):
+    """
+    Describes the BinaryOperator of a declaration
+    """
+
+    # The unique kind objects, index by id.
+    _kinds = []
+    _name_map = None
+
+    def __nonzero__(self):
+        """ Allows checks of the kind ```if cursor.binary_operator:```"""
+        return self.value != 0
+
+    @property
+    def is_assignment(self):
+        return BinaryOperator.Assign.value <= self.value < BinaryOperator.Comma.value
+
+    def __repr__(self):
+        return 'BinaryOperator.%s' % (self.name,)
+
+BinaryOperator.Invalid = BinaryOperator(0)
+BinaryOperator.PtrMemD = BinaryOperator(1)
----------------
AaronBallman wrote:

Despite looking like an off-by-one error, this is correct -- the code in CIndex.cpp does not cast `BinaryOperatorKind`, it uses a `switch` to map between values.

https://github.com/llvm/llvm-project/pull/98489


More information about the cfe-commits mailing list