Function pointer in a struct declaration


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?

Are there test cases or document that I can reference to figure out what kind of cursor or type I can get with various style of code? I am guessing there are test cases to verify the correct output of visitor, but a quick glance to the source code didn’t reveal them. I hope you experts would give me some pointers.

Thanks for the good work.



cursor.c (3.55 KB)

test_struct.output (3.44 KB)

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.

[2] clang_getResultType