[clang] [libclang/python] Make the value of a true bool enum 1, not -1 (PR #221497)
Jeff Trull via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 5 14:53:17 PDT 2026
https://github.com/jefftrull created https://github.com/llvm/llvm-project/pull/221497
When converted to int a bool value is either 0 or 1, but the enum_value of a bool enum was reported as either 0 or -1 in the Python bindings.
Make bool enums take the unsigned value code path so that 0 or +1 is returned instead.
If merged this will resolve #221326
>From 0a918c8d768656d926572755f705914371b6253f Mon Sep 17 00:00:00 2001
From: Jeff Trull <edaskel at att.net>
Date: Sat, 5 Sep 2026 14:48:24 -0700
Subject: [PATCH] Make the value of a true bool enum 1, not -1
When converted to int a bool value is either 0 or 1, but the
enum_value of a bool enum was reported as either 0 or -1 in the Python
bindings.
Make bool enums take the unsigned value code path so that 0 or +1 is
returned instead.
---
clang/bindings/python/clang/cindex.py | 1 +
.../bindings/python/tests/cindex/test_cursor.py | 17 +++++++++++++++++
2 files changed, 18 insertions(+)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index bc00dd770ce3b..80283f624fb96 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -2147,6 +2147,7 @@ def enum_value(self) -> int:
if underlying_type.kind == TypeKind.ENUM:
underlying_type = underlying_type.get_declaration().enum_type
if underlying_type.kind in (
+ TypeKind.BOOL,
TypeKind.CHAR_U,
TypeKind.UCHAR,
TypeKind.CHAR16,
diff --git a/clang/bindings/python/tests/cindex/test_cursor.py b/clang/bindings/python/tests/cindex/test_cursor.py
index 76680e576b307..c8ffa15c2e84b 100644
--- a/clang/bindings/python/tests/cindex/test_cursor.py
+++ b/clang/bindings/python/tests/cindex/test_cursor.py
@@ -705,6 +705,23 @@ def test_enum_values_unsigned(self):
self.assertEqual(ham.kind, CursorKind.ENUM_CONSTANT_DECL)
self.assertEqual(ham.enum_value, 200)
+ def test_enum_values_bool(self):
+ tu = get_tu("enum ON : bool { NO = false, YES = true };", lang="cpp")
+ enum = get_cursor(tu, "ON")
+ self.assertIsNotNone(enum)
+
+ self.assertEqual(enum.kind, CursorKind.ENUM_DECL)
+
+ enum_constants = list(enum.get_children())
+ self.assertEqual(len(enum_constants), 2)
+
+ no, yes = enum_constants
+
+ self.assertEqual(no.kind, CursorKind.ENUM_CONSTANT_DECL)
+ self.assertEqual(no.enum_value, 0)
+ self.assertEqual(yes.kind, CursorKind.ENUM_CONSTANT_DECL)
+ self.assertEqual(yes.enum_value, 1)
+
def test_annotation_attribute(self):
tu = get_tu(
'int foo (void) __attribute__ ((annotate("here be annotation attribute")));'
More information about the cfe-commits
mailing list