[clang] [libclang/python] Sync python kinds with Index.h enums (PR #143264)

Jannick Kremer via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 16 06:43:08 PDT 2025


================
@@ -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
----------------
DeinAlptraum wrote:

This was part of the builtin, but fortunately can be turned off. It now looks like this for the two directions:

```
======================================================================
FAIL: test_all_variants (tests.cindex.test_enums.TestEnums) [<enum 'TypeKind'>]
Check that all libclang enum values are also defined in cindex.py
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/shared/programming/repos/llvm-project/clang/bindings/python/tests/cindex/test_enums.py", line 86, in test_all_variants
    self.assertEqual(
AssertionError: {'CXType_OCLIntelSubgroupAVCImePayload', 'CXType_OCLIntelSubgroupAVCRefPayload', 'CXType_OCLImage2dMSAARW'} variants are missing. Please ensure these are defined in <enum 'TypeKind'> in cindex.py.

======================================================================
FAIL: test_all_variants (tests.cindex.test_enums.TestEnums) [<enum 'LinkageKind'>]
Check that all libclang enum values are also defined in cindex.py
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/shared/programming/repos/llvm-project/clang/bindings/python/tests/cindex/test_enums.py", line 97, in test_all_variants
    self.assertEqual(
AssertionError: {LinkageKind.THING, LinkageKind.SOME} variants only exist in the Python bindings. Please ensure that all <enum 'LinkageKind'> kinds defined in cindex.py have an equivalent in Index.h

```

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


More information about the cfe-commits mailing list