[cfe-dev] Fetching typedef details via libclang

Stefan Seefeld stefan at seefeld.name
Tue Apr 12 07:36:11 PDT 2011


On 2011-04-12 10:11, Douglas Gregor wrote:
> On Apr 11, 2011, at 11:18 PM, Abramo Bagnara wrote:
>
>> Il 12/04/2011 02:15, Douglas Gregor ha scritto:
>>> On Mar 28, 2011, at 1:55 PM, Stefan Seefeld wrote:
>>>
>>>> I'm looking for a way to query the libclang AST whether a typedef's
>>>> referree type was constructed in-place, such as in
>>>>
>>>>   typedef struct { ... } CXString;
>>>>
>>>>
>>>> Such constructs are rather common in C APIs, and I'd like to fold the
>>>> two types (the anonymous struct with the 'CXString' alias) into one, to
>>>> reduce the noise in the generated API documentation.
>>>>
>>>> Is this possible, using libclang ?
>>> Not currently.
>>>
>>>> If not, any ideas how best to
>>>> implement that ?
>>>
>>> It's a little tricky, because Clang's AST doesn't really hold on to this information well. In this specific case, where the tag is anonymous, you can query TagDecl::getTypedefForAnonDecl() to see if it's a typedef of an anonymous declaration. But in a similar case like
>>>
>>>   typedef struct CXString { ... } CXString;
>>>
>>> the AST doesn't have the information you seek. Perhaps this information could be added into ElaboratedType or ElaboratedTypeLoc.
>> You always forget isEmbeddedInDeclarator ;-)
> You're right, I did completely forget about this!
>
> So, this information is *obviously* available in the AST ;), but hasn't been exposed in libclang.

OK, so the obvious follow-up question (making sure that it doesn't get 
forgotten again :-) ):

What about this ?

/** \brief If the alias type definition is embedded in the declarator
  * of the typedef declaration pointed to by this cursor, return
  *  its cursor.
  */
CXCursor clang_getEmbeddedDefinition(CXCursor);

(I personally find this a little too compact, but I guess this is the 
price to pay for having a uniform Cursor type.)

Thanks,
         Stefan

-- 

       ...ich hab' noch einen Koffer in Berlin...




More information about the cfe-dev mailing list