Skip to content
Merged
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
21 changes: 18 additions & 3 deletions clang/bindings/python/clang/cindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2125,12 +2125,26 @@ def get_field_offsetof(self):

def is_anonymous(self):
"""
Check if the record is anonymous.
Check whether this is a record type without a name, or a field where
the type is a record type without a name.

Use is_anonymous_record_decl to check whether a record is an
"anonymous union" as defined in the C/C++ standard.
"""
if self.kind == CursorKind.FIELD_DECL:
return self.type.get_declaration().is_anonymous()
return conf.lib.clang_Cursor_isAnonymous(self) # type: ignore [no-any-return]

def is_anonymous_record_decl(self):
"""
Check if the record is an anonymous union as defined in the C/C++ standard
(or an "anonymous struct", the corresponding non-standard extension for
structs).
"""
if self.kind == CursorKind.FIELD_DECL:
return self.type.get_declaration().is_anonymous_record_decl()
return conf.lib.clang_Cursor_isAnonymousRecordDecl(self) # type: ignore [no-any-return]

def is_bitfield(self):
"""
Check if the field is a bitfield.
Expand Down Expand Up @@ -3902,12 +3916,13 @@ def write_main_file_to_stdout(self):
("clang_Cursor_getTemplateArgumentType", [Cursor, c_uint], Type),
("clang_Cursor_getTemplateArgumentValue", [Cursor, c_uint], c_longlong),
("clang_Cursor_getTemplateArgumentUnsignedValue", [Cursor, c_uint], c_ulonglong),
("clang_Cursor_isAnonymous", [Cursor], bool),
("clang_Cursor_isBitField", [Cursor], bool),
("clang_Cursor_getBinaryOpcode", [Cursor], c_int),
("clang_Cursor_getBriefCommentText", [Cursor], _CXString),
("clang_Cursor_getRawCommentText", [Cursor], _CXString),
("clang_Cursor_getOffsetOfField", [Cursor], c_longlong),
("clang_Cursor_isAnonymous", [Cursor], bool),
("clang_Cursor_isAnonymousRecordDecl", [Cursor], bool),
("clang_Cursor_isBitField", [Cursor], bool),
("clang_Location_isInSystemHeader", [SourceLocation], bool),
("clang_Type_getAlignOf", [Type], c_longlong),
("clang_Type_getClassType", [Type], Type),
Expand Down
3 changes: 3 additions & 0 deletions clang/bindings/python/tests/cindex/test_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,11 @@ def test_offset(self):
self.assertNotEqual(children[0].spelling, "typeanon")
self.assertEqual(children[1].spelling, "typeanon")
self.assertEqual(fields[0].kind, CursorKind.FIELD_DECL)
self.assertTrue(fields[0].is_anonymous())
self.assertFalse(fields[0].is_anonymous_record_decl())
self.assertEqual(fields[1].kind, CursorKind.FIELD_DECL)
self.assertTrue(fields[1].is_anonymous())
self.assertTrue(fields[1].is_anonymous_record_decl())
self.assertEqual(teststruct.type.get_offset("typeanon"), f1)
self.assertEqual(teststruct.type.get_offset("bariton"), bariton)
self.assertEqual(teststruct.type.get_offset("foo"), foo)
Expand Down
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,8 @@ Sanitizers
Python Binding Changes
----------------------
- Fixed an issue that led to crashes when calling ``Type.get_exception_specification_kind``.
- Added binding for ``clang_Cursor_isAnonymousRecordDecl``, which allows checking if
a declaration is an anonymous union or anonymous struct.

OpenMP Support
--------------
Expand Down
Loading