Skip to content

clangparser: Fix issue with duplicate defition of typedef'd enum - #158

Open
tkeeffe-cacdsp wants to merge 1 commit into
trolldbois:masterfrom
tkeeffe-cacdsp:fix-typedef-enum-duplicate-definition
Open

tkeeffe-cacdsp wants to merge 1 commit into
trolldbois:masterfrom
tkeeffe-cacdsp:fix-typedef-enum-duplicate-definition

Conversation

@tkeeffe-cacdsp

Copy link
Copy Markdown

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 empty .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 (always returns False), 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.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant