[cfe-dev] Function pointer in a struct declaration

Henry Jen henryjen at ztune.net
Fri Mar 27 14:28:44 PDT 2015


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
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150327/c8081af1/attachment.html>


More information about the cfe-dev mailing list