[cfe-dev] Function pointer in a struct declaration

Mukul Sabharwal mjsabby at gmail.com
Fri Mar 27 17:53:30 PDT 2015


This may help you, it's similar to your project description in that it
generates C# extern definitions (C# equivalent of JNI, I suppose):

https://github.com/mjsabby/ClangSharp/blob/master/ClangSharpPInvokeGenerator/TypeDefVisitor.cs#L27,#L75

The highlighted part is the relevant Function Pointer case where it checks
that is a pointer type and then its canonical pointee type which should be
a CXType_FunctionProto.

On Fri, Mar 27, 2015 at 2:28 PM, Henry Jen <henryjen at ztune.net> wrote:

> Calling clang_getCanonicalType on Unexposed works well, thanks for the tip.
>
> It would be nice to know what function can be used on what type with
> confidence without checking for invalid type all the time, so again,
> perhaps there are test cases or references in the code that can shed some
> light?
>
> Cheers,
> Henry
>
>
> Date: Fri, 27 Mar 2015 14:08:01 +0100
>
>> From: Jacob Carlborg <doob at me.com>
>> To: cfe-dev at cs.uiuc.edu
>> Subject: Re: [cfe-dev] Function pointer in a struct declaration
>> Message-ID: <mf3knh$3od$1 at ger.gmane.org>
>> Content-Type: text/plain; charset=windows-1252; format=flowed
>>
>> On 2015-03-27 07:20, Henry Jen wrote:
>> > Hi,
>> >
>> > 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
>>
>>
>>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150327/3fa547e7/attachment.html>


More information about the cfe-dev mailing list