Skip to content

Commit fbcf3cb

Browse files
[libclang/python] Add python binding for clang_Cursor_isAnonymousRecordDecl (#120483)
This function allows checking whether a declaration declares an anonymous union (as opposed to clang_Cursor_isAnonymous, which just checks if the declaration has a name).
1 parent bda7c9a commit fbcf3cb

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,12 +2125,26 @@ def get_field_offsetof(self):
21252125

21262126
def is_anonymous(self):
21272127
"""
2128-
Check if the record is anonymous.
2128+
Check whether this is a record type without a name, or a field where
2129+
the type is a record type without a name.
2130+
2131+
Use is_anonymous_record_decl to check whether a record is an
2132+
"anonymous union" as defined in the C/C++ standard.
21292133
"""
21302134
if self.kind == CursorKind.FIELD_DECL:
21312135
return self.type.get_declaration().is_anonymous()
21322136
return conf.lib.clang_Cursor_isAnonymous(self) # type: ignore [no-any-return]
21332137

2138+
def is_anonymous_record_decl(self):
2139+
"""
2140+
Check if the record is an anonymous union as defined in the C/C++ standard
2141+
(or an "anonymous struct", the corresponding non-standard extension for
2142+
structs).
2143+
"""
2144+
if self.kind == CursorKind.FIELD_DECL:
2145+
return self.type.get_declaration().is_anonymous_record_decl()
2146+
return conf.lib.clang_Cursor_isAnonymousRecordDecl(self) # type: ignore [no-any-return]
2147+
21342148
def is_bitfield(self):
21352149
"""
21362150
Check if the field is a bitfield.
@@ -3902,12 +3916,13 @@ def write_main_file_to_stdout(self):
39023916
("clang_Cursor_getTemplateArgumentType", [Cursor, c_uint], Type),
39033917
("clang_Cursor_getTemplateArgumentValue", [Cursor, c_uint], c_longlong),
39043918
("clang_Cursor_getTemplateArgumentUnsignedValue", [Cursor, c_uint], c_ulonglong),
3905-
("clang_Cursor_isAnonymous", [Cursor], bool),
3906-
("clang_Cursor_isBitField", [Cursor], bool),
39073919
("clang_Cursor_getBinaryOpcode", [Cursor], c_int),
39083920
("clang_Cursor_getBriefCommentText", [Cursor], _CXString),
39093921
("clang_Cursor_getRawCommentText", [Cursor], _CXString),
39103922
("clang_Cursor_getOffsetOfField", [Cursor], c_longlong),
3923+
("clang_Cursor_isAnonymous", [Cursor], bool),
3924+
("clang_Cursor_isAnonymousRecordDecl", [Cursor], bool),
3925+
("clang_Cursor_isBitField", [Cursor], bool),
39113926
("clang_Location_isInSystemHeader", [SourceLocation], bool),
39123927
("clang_Type_getAlignOf", [Type], c_longlong),
39133928
("clang_Type_getClassType", [Type], Type),

clang/bindings/python/tests/cindex/test_type.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,11 @@ def test_offset(self):
463463
self.assertNotEqual(children[0].spelling, "typeanon")
464464
self.assertEqual(children[1].spelling, "typeanon")
465465
self.assertEqual(fields[0].kind, CursorKind.FIELD_DECL)
466+
self.assertTrue(fields[0].is_anonymous())
467+
self.assertFalse(fields[0].is_anonymous_record_decl())
466468
self.assertEqual(fields[1].kind, CursorKind.FIELD_DECL)
467469
self.assertTrue(fields[1].is_anonymous())
470+
self.assertTrue(fields[1].is_anonymous_record_decl())
468471
self.assertEqual(teststruct.type.get_offset("typeanon"), f1)
469472
self.assertEqual(teststruct.type.get_offset("bariton"), bariton)
470473
self.assertEqual(teststruct.type.get_offset("foo"), foo)

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,8 @@ Sanitizers
12751275
Python Binding Changes
12761276
----------------------
12771277
- Fixed an issue that led to crashes when calling ``Type.get_exception_specification_kind``.
1278+
- Added binding for ``clang_Cursor_isAnonymousRecordDecl``, which allows checking if
1279+
a declaration is an anonymous union or anonymous struct.
12781280

12791281
OpenMP Support
12801282
--------------

0 commit comments

Comments
 (0)