-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libclang/python] Add python binding for clang_Cursor_isAnonymousRecordDecl #120483
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang Author: Eli Friedman (efriedma-quic) ChangesFull diff: https://github.com/llvm/llvm-project/pull/120483.diff 2 Files Affected:
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index f8a20a1e224724..b6e0d71c1ae1b9 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -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.
@@ -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),
diff --git a/clang/bindings/python/tests/cindex/test_type.py b/clang/bindings/python/tests/cindex/test_type.py
index ce05fdb1a1ebc0..e1d8c2aad1c3a4 100644
--- a/clang/bindings/python/tests/cindex/test_type.py
+++ b/clang/bindings/python/tests/cindex/test_type.py
@@ -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)
|
Endilll
approved these changes
Dec 19, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add a release note and a PR description.
LGTM otherwise.
DeinAlptraum
approved these changes
Dec 19, 2024
4a525f6
to
2267189
Compare
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).
2267189
to
ba71a3d
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).