[PATCH] libclang: functions to deal with anonymous fields

Loïc Jaquemet loic.jaquemet at gmail.com
Thu Dec 19 20:17:21 PST 2013


Hi,

visitChildren gives all type of children to the caller.
exemple:

struct Test {
  typedef struct {int a;} typeanon;
  struct {
    int bariton;
    };
  };
  int bar;
};

>From the python bindings:
>>> [ (child.kind,child.spelling) for child in teststruct.get_children()]
[(CursorKind.STRUCT_DECL, ''), (CursorKind.FIELD_DECL, 'typeanon'),
(CursorKind.STRUCT_DECL, ''), (CursorKind.FIELD_DECL, 'bar')]

>>> [ (child.kind,child.spelling) for child in teststruct.type.get_fields()]
[(CursorKind.FIELD_DECL, 'typeanon'), (CursorKind.FIELD_DECL, ''),
(CursorKind.FIELD_DECL, 'bar')]



When using  visitChildren, I found no logic to actually distinguish
record members is some corners cases like this one.
The first STRUCT_DEC is a typedef, child of struct Test, not a field.
The second STRUCT_DEC is an anonymous struct member.
There are all CursorKind that can be children of that cursor.
But only a subset is a FIELD_DECL.
And some fields are not of kind FIELD_DECL.
I can't find a way to make the difference.

Whereas with visitFields ( which is a clang piece of code ) no problem there.

visitFields is using the clang field iterator, which I think has valid
reasons to be exposed in libclang.

2013/12/19 Argyrios Kyrtzidis <akyrtzi at gmail.com>:
> Hi Loïc,
>
> About clang_Type_visitFields, can't the same functionality be achieved by
> getting the declaration cursor and using clang_visitChildren ?
>
> On Dec 15, 2013, at 2:34 PM, Loïc Jaquemet <loic.jaquemet at gmail.com> wrote:
>
> Hi,
>
> Nov 17 updated patch for 197355
>
> * 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.
>
> * TU in print-type
> - adds [nbFields] in c-index-test for records
> - for anonymous record, shows offset of field in anonymous and parent
> record. ( clang_Type_getOffsetOf/clang_Cursor_getOffsetOfField)
>
> 2013/11/17 Loïc Jaquemet <loic.jaquemet at gmail.com>:
>
> * libclang: Add three functions useful for dealing with anonymous
> fields.
> +clang_Cursor_getOffsetOfField
> +clang_Cursor_isAnonymous
> +clang_Type_visitFields
>
>
> Could you add a test, e.g. have c-index-test's '-test-print-type' print
> this info ?
>
>
> tests added in PrintType and PrintTypeSize.
> I also reorganised the validation code for
> clang_Cursor_getOffsetOfField and clang_Type_getOffsetOf
>
>
>
> * Python; Add corresponding methods for dealing with anonymous fields.
>
> * Python; Add the __contains__ method to SourceRange
> its a useful helper function.
> Plus it is useful to detect bug
> http://llvm.org/bugs/show_bug.cgi?id=17475
>
> 2013/10/12 Loïc Jaquemet <loic.jaquemet at gmail.com>:
>
> * Adds the __contains__ method to SourceRange
> It is useful to detect bug http://llvm.org/bugs/show_bug.cgi?id=17475
>
>
>
>
>
> --
> Loïc Jaquemet
>
>
>
>
> --
> Loïc Jaquemet
> <libclang-visit-fields-197355>
>
>



-- 
Loïc Jaquemet




More information about the cfe-commits mailing list