[cfe-dev] Getting the class name for a category in libclang

David Chisnall csdavec at swan.ac.uk
Fri Oct 29 12:46:05 PDT 2010


On 29 Oct 2010, at 20:20, Ted Kremenek wrote:

> On Oct 29, 2010, at 12:16 PM, David Chisnall wrote:
> 
>> Nope, this returns the name of the category.  For example, in the case of:
>> 
>> @implementation Foo (Bar)
>> 
>> This returns Bar.  I see no API for accessing Foo.
> 
> 
> Ah, I see.
> 
> When visiting the children of that cursor, I believe you should see a class reference to the @interface for 'Foo'.  You should be able to query the class name that way.  Does this work?  It's a little indirect, and possibly worthy of a wrapper API.


This sometimes works.  If there is an @implementation decl for the class visible, then there is an ObjCClassRef child.  If there is not, however, then the only children are the methods.  In the underlying APIs, it would simply be a call to ->getClassInterface() to get the interface.

There also doesn't seem to be a way of walking the class hierarchy; I was expecting to be able to find the superclass for a class interface decl, but the only way I can see for doing this is to find the token and then parse the source code myself, which rather defeats the point of using libclang...

I'm happy to spend some time implementing these, but we should probably agree on what the interfaces should look like first.  I would propose something like this:

// Returns the cursor for the class @interface when passed a category @interface or @implementation cursor, invalid cursor otherwise.

CXCursor clang_getObjCCategoryClassInterface(CXCursor);

// Returns the number of superclasses of the given class (always returns 0 or 1 for ObjC classes, may return more for C++).  Returns 0 if the cursor is not a valid ObjC or C++ class interface or implementation decl

unsigned clang_getNumberOfSuperclasses(CXCursor);

// Returns a cursor for the interface to the nth superclass of the cursor indicated by the first argument.

CXCursor clang_getSuperclass(CXCursor, unsigned);

David

-- Sent from my Difference Engine




More information about the cfe-dev mailing list