Skip to content

Commit 2cec7bf

Browse files
authored
Merge pull request #138 from dgarnier/fix_anon_unions
anonymous structs and unions fix
2 parents d9d72a7 + 0c36d3f commit 2cec7bf

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

ctypeslib/codegen/cursorhandler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,11 @@ def FIELD_DECL(self, cursor):
985985
# Note: cursor.is_anonymous seems to be unreliable/inconsistent across
986986
# libclang versions, and we will consider the field as anonymous if
987987
# cursor.spelling is empty
988+
# but at least with clang-17.. anonymous fields have a name "type (anonymous at ..)"
988989
name = cursor.spelling
989990
offset = parent.type.get_offset(name)
991+
if "(anonymous" in name:
992+
name = ""
990993
if not name and cursor.is_anonymous() and not cursor.is_bitfield():
991994
# anonymous type, that is not a bitfield field case:
992995
offset = cursor.get_field_offsetof()

ctypeslib/codegen/handler.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Abstract Handler with helper methods."""
22

3-
from clang.cindex import CursorKind, TypeKind
3+
from clang.cindex import CursorKind, TypeKind, Cursor
44

55
from ctypeslib.codegen import typedesc
66
from ctypeslib.codegen.util import log_entity
@@ -127,13 +127,21 @@ def _make_unknown_name(self, cursor, field_name):
127127

128128
def get_unique_name(self, cursor, field_name=None):
129129
"""get the spelling or create a unique name for a cursor"""
130+
# this gets called for both cursors and types!
131+
# so cursor.kind can be a CursorKind or a TypeKind
130132
if cursor.kind in [CursorKind.UNEXPOSED_DECL]:
131133
return ''
132134
# covers most cases
133135
name = cursor.spelling
134136
if cursor.kind == CursorKind.CXX_BASE_SPECIFIER:
135137
name = cursor.type.spelling
136138
# if it's a record decl or field decl and its type is anonymous
139+
# clang > 16 changes anonymous names to have a parenthetical name
140+
# so force it to have blank name like it did in earlier clang versions
141+
# only cursors, not types have .is_anonymous()
142+
if (isinstance(cursor.kind, CursorKind) and
143+
cursor.is_anonymous() and '(' in name):
144+
name = ''
137145
if name == '':
138146
# if cursor.is_anonymous():
139147
# a unnamed object at the root TU

ctypeslib/codegen/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def log_entity(func):
114114
def fn(*args, **kwargs):
115115
name = args[0].get_unique_name(args[1])
116116
if name == '':
117-
parent = args[1].semantic_parent
117+
parent = getattr(args[1], 'semantic_parent', None)
118118
if parent:
119119
name = 'child of %s' % parent.displayname
120120
log.debug("%s: displayname:'%s'",func.__name__, name)

0 commit comments

Comments
 (0)