diff options
author | Francois Pichet <pichet2000@gmail.com> | 2015-01-29 12:45:29 +0000 |
---|---|---|
committer | Francois Pichet <pichet2000@gmail.com> | 2015-01-29 12:45:29 +0000 |
commit | fa8a3bc3d030a3e675540b653d4084f44042d09d (patch) | |
tree | 6e5d09565f8652149ffae65d324b6293edcfb5b3 /bindings | |
parent | 2a27967cd6072877cd42dba59bea170561284e5e (diff) |
libclang: Add three functions useful for dealing with anonymous fields:
clang_Cursor_getOffsetOfField
clang_Cursor_isAnonymous
clang_Type_visitFields
Python: Add corresponding methods for dealing with anonymous fields.
Patch by Loïc Jaquemet
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@227432 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings')
-rw-r--r-- | bindings/python/clang/cindex.py | 40 | ||||
-rw-r--r-- | bindings/python/tests/cindex/test_type.py | 21 |
2 files changed, 55 insertions, 6 deletions
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py index 5792effea5..f5caca8572 100644 --- a/bindings/python/clang/cindex.py +++ b/bindings/python/clang/cindex.py @@ -1476,6 +1476,18 @@ class Cursor(Structure): """ return TokenGroup.get_tokens(self._tu, self.extent) + def get_field_offsetof(self): + """Returns the offsetof the FIELD_DECL pointed by this Cursor.""" + return conf.lib.clang_Cursor_getOffsetOfField(self) + + def is_anonymous(self): + """ + Check if the record is anonymous. + """ + if self.kind == CursorKind.FIELD_DECL: + return self.type.get_declaration().is_anonymous() + return conf.lib.clang_Cursor_isAnonymous(self) + def is_bitfield(self): """ Check if the field is a bitfield. @@ -1884,6 +1896,21 @@ class Type(Structure): return RefQualifierKind.from_id( conf.lib.clang_Type_getCXXRefQualifier(self)) + def get_fields(self): + """Return an iterator for accessing the fields of this type.""" + + def visitor(field, children): + assert field != conf.lib.clang_getNullCursor() + + # Create reference to TU so it isn't GC'd before Cursor. + field._tu = self._tu + fields.append(field) + return 1 # continue + fields = [] + conf.lib.clang_Type_visitFields(self, + callbacks['fields_visit'](visitor), fields) + return iter(fields) + @property def spelling(self): """Retrieve the spelling of this Type.""" @@ -2780,6 +2807,7 @@ class Token(Structure): callbacks['translation_unit_includes'] = CFUNCTYPE(None, c_object_p, POINTER(SourceLocation), c_uint, py_object) callbacks['cursor_visit'] = CFUNCTYPE(c_int, Cursor, Cursor, py_object) +callbacks['fields_visit'] = CFUNCTYPE(c_int, Cursor, py_object) # Functions strictly alphabetical order. functionList = [ @@ -3367,6 +3395,10 @@ functionList = [ [Cursor, c_uint], c_ulonglong), + ("clang_Cursor_isAnonymous", + [Cursor], + bool), + ("clang_Cursor_isBitField", [Cursor], bool), @@ -3381,6 +3413,10 @@ functionList = [ _CXString, _CXString.from_result), + ("clang_Cursor_getOffsetOfField", + [Cursor], + c_longlong), + ("clang_Type_getAlignOf", [Type], c_longlong), @@ -3401,6 +3437,10 @@ functionList = [ ("clang_Type_getCXXRefQualifier", [Type], c_uint), + + ("clang_Type_visitFields", + [Type, callbacks['fields_visit'], py_object], + c_uint), ] class LibclangError(Exception): diff --git a/bindings/python/tests/cindex/test_type.py b/bindings/python/tests/cindex/test_type.py index a02c06fe5a..f3dadf999b 100644 --- a/bindings/python/tests/cindex/test_type.py +++ b/bindings/python/tests/cindex/test_type.py @@ -363,6 +363,7 @@ def test_offset(): """Ensure Cursor.get_record_field_offset works in anonymous records""" source=""" struct Test { + struct {int a;} typeanon; struct { int bariton; union { @@ -371,15 +372,23 @@ struct Test { }; int bar; };""" - tries=[(['-target','i386-linux-gnu'],(4,16,0,32,64)), - (['-target','nvptx64-unknown-unknown'],(8,24,0,32,64)), - (['-target','i386-pc-win32'],(8,16,0,32,64)), - (['-target','msp430-none-none'],(2,14,0,32,64))] + tries=[(['-target','i386-linux-gnu'],(4,16,0,32,64,96)), + (['-target','nvptx64-unknown-unknown'],(8,24,0,32,64,96)), + (['-target','i386-pc-win32'],(8,16,0,32,64,96)), + (['-target','msp430-none-none'],(2,14,0,32,64,96))] for flags, values in tries: - align,total,bariton,foo,bar = values + align,total,f1,bariton,foo,bar = values tu = get_tu(source) teststruct = get_cursor(tu, 'Test') - fields = list(teststruct.get_children()) + children = list(teststruct.get_children()) + fields = list(teststruct.type.get_fields()) + assert children[0].kind == CursorKind.STRUCT_DECL + assert children[0].spelling != "typeanon" + assert children[1].spelling == "typeanon" + assert fields[0].kind == CursorKind.FIELD_DECL + assert fields[1].kind == CursorKind.FIELD_DECL + assert fields[1].is_anonymous() + assert teststruct.type.get_offset("typeanon") == f1 assert teststruct.type.get_offset("bariton") == bariton assert teststruct.type.get_offset("foo") == foo assert teststruct.type.get_offset("bar") == bar |