[clang] [libclang/python] Add missing enum variants (PR #143264)
Jannick Kremer via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 15 07:30:59 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
----------------
DeinAlptraum wrote:
> can we replace the registry of enums that contributors and reviewers are prone to forget about, and inspect the module instead?
That's a great idea, and changing that made me realize we were also missing the `PrintingPolicyProperty`.
> This code ignores enums that are present in Index.h but not in self.enums
As for this: good idea. This already fails because for some reason, our `AccessSpecifier` enum has a fourth kind `NONE` that was introduced right at the start (see https://github.com/llvm/llvm-project/commit/05f9613610aba1f041e79a2efbb4dc32ebaa3e70) and doesn't/shouldn't be used.
https://github.com/llvm/llvm-project/pull/143264
More information about the cfe-commits
mailing list