[clang] [libclang/python] Add missing enum variants (PR #143264)
Jannick Kremer via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 7 07:37:01 PDT 2025
https://github.com/DeinAlptraum updated https://github.com/llvm/llvm-project/pull/143264
>From 71a404a5bbef81fa0aba48eb14671ed17ce4d8fb Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Sat, 7 Jun 2025 21:57:17 +0900
Subject: [PATCH 1/2] [libclang/python] Add missing enum variants
Add tests to ensure that all C-enum variants are defined on Python side.
---
.../python/tests/cindex/test_enums.py | 49 ++++++++++++++++++-
1 file changed, 48 insertions(+), 1 deletion(-)
diff --git a/clang/bindings/python/tests/cindex/test_enums.py b/clang/bindings/python/tests/cindex/test_enums.py
index 9e7f44fcf7867..54c0c77e4df29 100644
--- a/clang/bindings/python/tests/cindex/test_enums.py
+++ b/clang/bindings/python/tests/cindex/test_enums.py
@@ -1,4 +1,5 @@
import unittest
+from pathlib import Path
from clang.cindex import (
AccessSpecifier,
@@ -12,6 +13,7 @@
TemplateArgumentKind,
TLSKind,
TokenKind,
+ TranslationUnit,
TypeKind,
)
@@ -44,8 +46,53 @@ def test_from_id(self):
def test_duplicate_ids(self):
"""Check that no two kinds have the same id"""
- # for enum in self.enums:
for enum in self.enums:
num_declared_variants = len(enum._member_map_.keys())
num_unique_variants = len(list(enum))
self.assertEqual(num_declared_variants, num_unique_variants)
+
+ def test_all_variants(self):
+ """Check that all libclang enum values are also defined in cindex"""
+ cenum_to_pythonenum = {
+ "CX_CXXAccessSpecifier": AccessSpecifier,
+ "CXAvailabilityKind": AvailabilityKind,
+ "CXBinaryOperatorKind": BinaryOperator,
+ "CXCursorKind": CursorKind,
+ "CXCursor_ExceptionSpecificationKind": ExceptionSpecificationKind,
+ "CXLinkageKind": LinkageKind,
+ "CXRefQualifierKind": RefQualifierKind,
+ "CX_StorageClass": StorageClass,
+ "CXTemplateArgumentKind": TemplateArgumentKind,
+ "CXTLSKind": TLSKind,
+ "CXTokenKind": TokenKind,
+ "CXTypeKind": TypeKind,
+ }
+
+ indexheader = (
+ Path(__file__).parent.parent.parent.parent.parent
+ / "include/clang-c/Index.h"
+ )
+ tu = TranslationUnit.from_source(indexheader, ["-x", "c++"])
+
+ enum_variant_map = {}
+ # For all enums in self.enums, extract all enum variants defined in Index.h
+ for cursor in tu.cursor.walk_preorder():
+ type_class = cenum_to_pythonenum.get(cursor.type.spelling)
+ if (
+ cursor.kind == CursorKind.ENUM_CONSTANT_DECL
+ and type_class in self.enums
+ ):
+ if type_class not in enum_variant_map:
+ enum_variant_map[type_class] = []
+ enum_variant_map[type_class].append(cursor.enum_value)
+
+ for enum in self.enums:
+ with self.subTest(enum):
+ python_kinds = set([kind.value for kind in enum])
+ c_kinds = set(enum_variant_map[enum])
+ missing_python_kinds = c_kinds - python_kinds
+ self.assertEqual(
+ missing_python_kinds,
+ set(),
+ f"Please ensure these variants are defined inside {enum} in cindex.py.",
+ )
>From fc97cbc8eeb6453ee7aa5460ded74319d4097f9b Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Sat, 7 Jun 2025 23:29:01 +0900
Subject: [PATCH 2/2] Add new variants
---
clang/bindings/python/clang/cindex.py | 62 ++++++++++++++++++-
.../python/tests/cindex/test_enums.py | 2 +-
2 files changed, 61 insertions(+), 3 deletions(-)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 824674309d262..40da7479656c2 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -707,7 +707,6 @@ def is_unexposed(self):
"""Test if this is an unexposed kind."""
return conf.lib.clang_isUnexposed(self) # type: ignore [no-any-return]
-
###
# Declaration Kinds
@@ -834,7 +833,6 @@ def is_unexposed(self):
# A C++ access specifier decl.
CXX_ACCESS_SPEC_DECL = 39
-
###
# Reference Kinds
@@ -1435,12 +1433,60 @@ def is_unexposed(self):
# OpenMP scope directive.
OMP_SCOPE_DIRECTIVE = 306
+ # OpenMP reverse directive.
+ OMPReverseDirective = 307
+
+ # OpenMP interchange directive.
+ OMPInterchangeDirective = 308
+
+ # OpenMP assume directive.
+ OMPAssumeDirective = 309
+
# OpenMP stripe directive.
OMP_STRIPE_DIRECTIVE = 310
# OpenACC Compute Construct.
OPEN_ACC_COMPUTE_DIRECTIVE = 320
+ # OpenACC Loop Construct.
+ OpenACCLoopConstruct = 321
+
+ # OpenACC Combined Constructs.
+ OpenACCCombinedConstruct = 322
+
+ # OpenACC data Construct.
+ OpenACCDataConstruct = 323
+
+ # OpenACC enter data Construct.
+ OpenACCEnterDataConstruct = 324
+
+ # OpenACC exit data Construct.
+ OpenACCExitDataConstruct = 325
+
+ # OpenACC host_data Construct.
+ OpenACCHostDataConstruct = 326
+
+ # OpenACC wait Construct.
+ OpenACCWaitConstruct = 327
+
+ # OpenACC init Construct.
+ OpenACCInitConstruct = 328
+
+ # OpenACC shutdown Construct.
+ OpenACCShutdownConstruct = 329
+
+ # OpenACC set Construct.
+ OpenACCSetConstruct = 330
+
+ # OpenACC update Construct.
+ OpenACCUpdateConstruct = 331
+
+ # OpenACC atomic Construct.
+ OpenACCAtomicConstruct = 332
+
+ # OpenACC cache Construct.
+ OpenACCCacheConstruct = 333
+
###
# Other Kinds
@@ -1559,6 +1605,7 @@ class ExceptionSpecificationKind(BaseEnumeration):
UNEVALUATED = 6
UNINSTANTIATED = 7
UNPARSED = 8
+ NOTHROW = 9
### Cursors ###
@@ -2482,6 +2529,13 @@ def spelling(self):
FLOAT128 = 30
HALF = 31
FLOAT16 = 32
+ SHORTACCUM = 33
+ ACCUM = 34
+ LONGACCUM = 35
+ USHORTACCUM = 36
+ UACCUM = 37
+ ULONGACCUM = 38
+ BFLOAT16 = 39
IBM128 = 40
COMPLEX = 100
POINTER = 101
@@ -2566,6 +2620,10 @@ def spelling(self):
ATOMIC = 177
BTFTAGATTRIBUTED = 178
+ HLSLRESOURCE = 179
+ HLSLATTRIBUTEDRESOURCE = 180
+ HLSLINLINESPIRV = 181
+
class RefQualifierKind(BaseEnumeration):
"""Describes a specific ref-qualifier of a type."""
diff --git a/clang/bindings/python/tests/cindex/test_enums.py b/clang/bindings/python/tests/cindex/test_enums.py
index 54c0c77e4df29..93e6c8db4bd42 100644
--- a/clang/bindings/python/tests/cindex/test_enums.py
+++ b/clang/bindings/python/tests/cindex/test_enums.py
@@ -94,5 +94,5 @@ def test_all_variants(self):
self.assertEqual(
missing_python_kinds,
set(),
- f"Please ensure these variants are defined inside {enum} in cindex.py.",
+ f"Please ensure these are defined in {enum} in cindex.py.",
)
More information about the cfe-commits
mailing list