Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions ctypeslib/codegen/clangparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 9 additions & 0 deletions test/data/test-enum-typedef.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "test-enum-typedef.h"

void abc1(abc_t e) {
return;
}

void abc2(abc_t e) {
return;
}
8 changes: 8 additions & 0 deletions test/data/test-enum-typedef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
typedef enum {
A,
B,
C
} abc_t;

void abc1(abc_t e);
void abc2(abc_t e);
7 changes: 7 additions & 0 deletions test/test_clang2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down