https://github.com/DeinAlptraum updated https://github.com/llvm/llvm-project/pull/143264
>From cee0067c7a0b35d2e1f15fd3fa698ec2afd6de88 Mon Sep 17 00:00:00 2001 From: Jannick Kremer <jannick.kre...@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 808ea3806632bc5924b71a7f4462c9e1756768ee Mon Sep 17 00:00:00 2001 From: Jannick Kremer <jannick.kre...@mailbox.org> Date: Sat, 7 Jun 2025 23:29:01 +0900 Subject: [PATCH 2/2] Add new variants --- clang/bindings/python/clang/cindex.py | 61 +++++++++++++++++++ .../python/tests/cindex/test_enums.py | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 6f7243cdf80ac..3480d72cd29bb 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -1435,12 +1435,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 +1607,7 @@ class ExceptionSpecificationKind(BaseEnumeration): UNEVALUATED = 6 UNINSTANTIATED = 7 UNPARSED = 8 + NOTHROW = 9 ### Cursors ### @@ -2483,6 +2532,14 @@ def spelling(self): OBJCSEL = 29 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 @@ -2567,6 +2624,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.", ) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits