================ @@ -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:
Tbh I don't remember why I did this. Comparing the numbers rather than the names is certainly more resilient, but there's nothing stopping me from converting those back to names after the comparison. Changed this accordingly. An example for a failure in each direction (C <-> Python kinds) now looks like this: ``` FAIL: test_all_variants (tests.cindex.test_enums.TestEnums) [<enum 'BinaryOperator'>] 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 88, in test_all_variants self.assertEqual( AssertionError: Items in the first set but not the second: 'CXBinaryOperator_LAnd' 'CXBinaryOperator_LOr' : Please ensure these are defined in <enum 'BinaryOperator'> in cindex.py. ====================================================================== FAIL: test_all_variants (tests.cindex.test_enums.TestEnums) [<enum 'AccessSpecifier'>] 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 98, in test_all_variants self.assertEqual( AssertionError: Items in the first set but not the second: AccessSpecifier.NONE : Please ensure that all <enum 'AccessSpecifier'> kinds defined in cindex.py have an equivalent in Index.h ``` https://github.com/llvm/llvm-project/pull/143264 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits