From d0acad164c7dcf23c8c3543535218b95d52a9558 Mon Sep 17 00:00:00 2001 From: Thomas O'Keeffe Date: Wed, 28 Oct 2026 15:00:30 -0400 Subject: [PATCH] clangparser: Fix issue with duplicate defition of typedef'd enum When an enum of the form `typedef enum { A, B, C } abc_t` is parsed as part of a source file that is listed in the clang2py's arguments, the enum is parsed registered first, independently from the typedef, then the typedef is parsed completely after the enum is done. This doesn't cause issues because the TYPEDEF_DECL handler checks for a cached type first. However, when parsed as part of an included header, the typedef is parsed first and the enum is parsed as a result of the typedef being parsed. This happens *after* the cache check happens, but before the typedef is registered. Thus when the typedef is registered, a DuplicateDefinitionException is raised. Note that this behavior is different than structs because untagged enums that are part of a typedef do NOT have an .name or .spelling field like an anonymous struct would. Clang inserts the typedef name into the enum declaration for quality of life or debugging purposes or something. Further, is_anonymous() doesn't work on enums, only structs and unions. The fix is to allow the register() call to return the registered enum when called with a typedef where the typedef'd object is the same as the enum object. The added test (test_enum_typedef) ensures that the output of the bug reproduction (test/data/test-enum-typedef.{c,h}) is identical whether the header is parsed directly from the argument list or as an include. --- ctypeslib/codegen/clangparser.py | 18 +++++++++++------- test/data/test-enum-typedef.c | 9 +++++++++ test/data/test-enum-typedef.h | 8 ++++++++ test/test_clang2py.py | 7 +++++++ 4 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 test/data/test-enum-typedef.c create mode 100644 test/data/test-enum-typedef.h diff --git a/ctypeslib/codegen/clangparser.py b/ctypeslib/codegen/clangparser.py index 5c92a06..b298082 100644 --- a/ctypeslib/codegen/clangparser.py +++ b/ctypeslib/codegen/clangparser.py @@ -208,15 +208,19 @@ def register(self, name, obj): log.debug("register: %s already defined: %s", name, obj.name) return self.all[name] if name in self.all: - if not isinstance(self.all[name], typedesc.Structure) or (self.all[name].members is not None): - # code.interact(local=locals()) - raise DuplicateDefinitionException( - f"register: {name} which has a previous incompatible definition: {obj.name}" - f"\ndefined here: {obj.location}" - f"\npreviously defined here: {self.all[name].location}" - ) if isinstance(self.all[name], typedesc.Structure) and (self.all[name].members is None): return obj + if isinstance(obj, typedesc.Typedef) and obj.typ == self.all[name]: + # covers parsing order edge case where typedef is parsed before + # inline enum (or other) declaration. See test/data/test-enum-typedef.{c,h} + return self.all[name] + + # code.interact(local=locals()) + raise DuplicateDefinitionException( + f"register: {name} which has a previous incompatible definition: {obj.name}" + f"\ndefined here: {obj.location}" + f"\npreviously defined here: {self.all[name].location}" + ) log.debug("register: %s ", name) self.all[name] = obj self.all_set.add((name, obj)) diff --git a/test/data/test-enum-typedef.c b/test/data/test-enum-typedef.c new file mode 100644 index 0000000..b49da9b --- /dev/null +++ b/test/data/test-enum-typedef.c @@ -0,0 +1,9 @@ +#include "test-enum-typedef.h" + +void abc1(abc_t e) { + return; +} + +void abc2(abc_t e) { + return; +} diff --git a/test/data/test-enum-typedef.h b/test/data/test-enum-typedef.h new file mode 100644 index 0000000..145a37d --- /dev/null +++ b/test/data/test-enum-typedef.h @@ -0,0 +1,8 @@ +typedef enum { + A, + B, + C +} abc_t; + +void abc1(abc_t e); +void abc2(abc_t e); diff --git a/test/test_clang2py.py b/test/test_clang2py.py index b9673b3..da20bab 100644 --- a/test/test_clang2py.py +++ b/test/test_clang2py.py @@ -308,6 +308,13 @@ def test_enum_struct(self): enum = output.index("E = ctypes.c_uint32") self.assertGreater(decl, enum, "Generated incorrect ordering") + def test_enum_typedef(self): + p, c_output, _ = clang2py(['./test/data/test-enum-typedef.c']) + self.assertEqual(0, p.returncode) + p, h_output, _ = clang2py(['./test/data/test-enum-typedef.h']) + self.assertEqual(0, p.returncode) + self.assertEqual(c_output, h_output) + class TestLocation(ClangTest):