Fetching typedef details via libclang

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 ? If not, any ideas how best to implement that ?

Thanks,
    Stefan

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.

  - Doug

Doug,

thanks. The case I was most interested in is the "typedef struct {...} CXString;" case, since this is a pretty common idiom in C, and would result in lots of anonymous structs in the documentation. My hack in Synopsis is to check whether the first token in the declaration is "typedef", and whether the struct is in fact anonymous. If both is true, I fold the two into one.

That seems to work reasonably well (see http://synopsis.fresco.org/clang/libclang for example).

FWIW,
         Stefan

You always forget isEmbeddedInDeclarator :wink:

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.

  - Doug

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

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