[cfe-dev] Function pointer in a struct declaration

Jacob Carlborg doob at me.com
Fri Mar 27 06:08:01 PDT 2015


On 2015-03-27 07:20, Henry Jen wrote:
> Hi,
>
> In OpenJDK project panama, we are working on a tool to take in a header
> file and spit out Java APIs utilizing libclang. A proof-of-concept can
> be found at [1].
>
> I would like to get the function type if a field of struct is a function
> pointer, but I am not sure how to do that. I was expecting to get a
> pointee type of FunctionProto on that field, but looks like I only get
> an Unexposed.
>
> Attached are a cursor visitor code I did to dump cursors and their type,
> and the result of the dump for following snippet.
>
> typedef struct foo {
>      int(*fn)(char* arg1, long arg2);
>      struct bar *ptr;
> } foo;
>
> I got a FieldDecl for fn, which has a type of Pointer whose pointeeType
> is Unexposed. In case of struct, pointee type is also Unexposed, but
> there is a TypeRef and StructDecl for bar, so at least I can figureout
> there is a Record type, but how figure out the function type for fn?

This is the way I identify a function pointer in my code [1]:

1. If the cursor is a pointer
2. And if the result type [2] of the ponitee type is valid, assume it's 
a function pointer

"is valid" in this context would mean that the type kind is not 
CXType_Invalid.

It's a workaround that has so far not caused any problems for me. I'm 
not sure if there's a better way to do this.

Actually, I just noticed that the canonical type kind of the pointee 
type is CXType_FunctionProto. That looks like a better approach.

[1] https://github.com/jacob-carlborg/dstep/blob/master/clang/Type.d#L52-L65
[2] clang_getResultType

-- 
/Jacob Carlborg




More information about the cfe-dev mailing list