[clang] [libclang/python] Make the value of a true bool enum 1, not -1 (PR #221497)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 5 14:54:01 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Jeff Trull (jefftrull)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/221497.diff
2 Files Affected:
- (modified) clang/bindings/python/clang/cindex.py (+1)
- (modified) clang/bindings/python/tests/cindex/test_cursor.py (+17)
``````````diff
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")));'
``````````
</details>
https://github.com/llvm/llvm-project/pull/221497
More information about the cfe-commits
mailing list