[cfe-dev] What is a TypeRef, and how do you use it? (clang-c)

Doug douglas.linder at gmail.com
Sat Nov 26 20:27:21 PST 2011


Hi again,

Having a few more dramas with the clang-c api. Particularly Typedef's seem
to behave oddly.

If I do a dump of the ast for this code:

struct x {
  int y;
};

typedef struct x MyThing;

extern MyThing *example;

I get something along the lines of this from the parser:

Symbol: x, Type: struct, Raw Kind: CXCursor_StructDecl
Symbol: y, Type: int, Raw Kind: CXCursor_FieldDecl
Symbol: MyThing, Type: record, Raw Kind: CXCursor_TypedefDecl
Symbol: struct x, Type: record, Raw Kind: CXCursor_TypeRef
Symbol: example, Type: record *, Raw Kind: CXCursor_VarDecl
Symbol: MyThing, Type: record, Raw Kind: CXCursor_TypeRef

In a slightly better format that's basically:

x (struct) -> y (field)
MyThing (Typedef)
struct x (Typeref)
example (Var)
MyThing (Typeref)

-_-

What's going on here? Why is there a trailing Typeref symbol?

The docs from TypeRef say:

  /**
   * \brief A reference to a type declaration.
   *
   * A type reference occurs anywhere where a type is named but not
   * declared. For example, given:
   *
   * \code
   * typedef unsigned size_type;
   * size_type size;
   * \endcode
   *
   * The typedef is a declaration of size_type (CXCursor_TypedefDecl),
   * while the type of the variable "size" is referenced. The cursor
   * referenced by the type of size is the typedef for size_type.
   */

So I suppose I'm supposed to use clang_getTypeDeclaration() on the Typeref
to get the cursor for the typedef for something.

Fair enough; I can use that from the 'struct x' TypeRef -> the MyType
typedef.


However, how can I relate the 'example' var to the related typedef?

I presume that there is supposed to be some way to relate the trailing
MyType --> example, and then MyType ---> the typedef.


However, if I do a getSemanticParent() on the two nodes 'example' and
'MyType' (trailing) I get:

'example' -> (getSemanticParent) --> CXCursor_UnexposedDecl
'MyType' ->(getSemanticParent) --> CXCursor_FirstInvalid

so....

What now?

Is this just not supported?


help much appreciated!

cheers,
Doug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20111127/8c47d0f4/attachment.html>


More information about the cfe-dev mailing list