Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anonymous structs and unions fix #138

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions ctypeslib/codegen/cursorhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,11 @@ def FIELD_DECL(self, cursor):
# Note: cursor.is_anonymous seems to be unreliable/inconsistent across
# libclang versions, and we will consider the field as anonymous if
# cursor.spelling is empty
# but at least with clang-17.. anonymous fields have a name "type (anonymous at ..)"
name = cursor.spelling
offset = parent.type.get_offset(name)
if "(anonymous" in name:
name = ""
if not name and cursor.is_anonymous() and not cursor.is_bitfield():
# anonymous type, that is not a bitfield field case:
offset = cursor.get_field_offsetof()
Expand Down
10 changes: 9 additions & 1 deletion ctypeslib/codegen/handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Abstract Handler with helper methods."""

from clang.cindex import CursorKind, TypeKind
from clang.cindex import CursorKind, TypeKind, Cursor

from ctypeslib.codegen import typedesc
from ctypeslib.codegen.util import log_entity
Expand Down Expand Up @@ -126,13 +126,21 @@ def _make_unknown_name(self, cursor, field_name):

def get_unique_name(self, cursor, field_name=None):
"""get the spelling or create a unique name for a cursor"""
# this gets called for both cursors and types!
# so cursor.kind can be a CursorKind or a TypeKind
if cursor.kind in [CursorKind.UNEXPOSED_DECL]:
return ''
# covers most cases
name = cursor.spelling
if cursor.kind == CursorKind.CXX_BASE_SPECIFIER:
name = cursor.type.spelling
# if it's a record decl or field decl and its type is anonymous
# clang > 16 changes anonymous names to have a parenthetical name
# so force it to have blank name like it did in earlier clang versions
# only cursors, not types have .is_anonymous()
if (isinstance(cursor.kind, CursorKind) and
cursor.is_anonymous() and '(' in name):
name = ''
if name == '':
# if cursor.is_anonymous():
# a unnamed object at the root TU
Expand Down
2 changes: 1 addition & 1 deletion ctypeslib/codegen/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def log_entity(func):
def fn(*args, **kwargs):
name = args[0].get_unique_name(args[1])
if name == '':
parent = args[1].semantic_parent
parent = getattr(args[1], 'semantic_parent', None)
if parent:
name = 'child of %s' % parent.displayname
log.debug("%s: displayname:'%s'",func.__name__, name)
Expand Down