[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 16:34:09 PDT 2026
https://github.com/jefftrull updated https://github.com/llvm/llvm-project/pull/221497
>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 1/2] 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")));'
>From 27877961d50232e1d2affeba978223d8674720e8 Mon Sep 17 00:00:00 2001
From: Jeff Trull <edaskel at att.net>
Date: Sat, 5 Sep 2026 16:33:15 -0700
Subject: [PATCH 2/2] Add a suitable release note for the enum_value change
---
clang/docs/ReleaseNotes.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 1686537008499..96f24fd2f8e9b 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -133,6 +133,8 @@ features cannot lower the translation-unit ABI level;
As a result, the `__str__` representation of its return values changed.
Like other libclang enums, it now follows the `CompletionChunkKind.VARIANT_NAME` scheme instead of `VariantName`.
+- `Cursor` instance's `enum_value` method now returns 1 instead of -1 for `true` bool enumeration values
+
### OpenCL Potentially Breaking Changes
## What's New in Clang {{env.config.release}}?
More information about the cfe-commits
mailing list