[cfe-dev] extracting name of type with cindex

Mark Moreno kramonerom at gmail.com
Tue May 8 08:52:22 PDT 2012


Hello,

let's say I have:

class Test {
  int foo;
  vector<string> bar;
  char* ptr;
};

with cindex, I want to extract the variable name and data type from
this class, eg, I want to have 'int', 'foo', 'vector<string>', 'bar',
'char*', 'ptr'.

Getting into the class with a cursor seems easy, as getting the
variable, but how do I get the original representation of the type?

If I use something like:

print >>output, cursor.type.kind.spelling,
cursor.type.get_declaration().spelling, cursor.spelling, cursor.kind,
cursor.kind.name

I get back:

Int None foo CursorKind.FIELD_DECL FIELD_DECL
Unexposed vector bar CursorKind.FIELD_DECL FIELD_DECL
Pointer None ptr CursorKind.FIELD_DECL FIELD_DECL

Any easy way to get the original representation of the type? or at
least reconstruct what it should be like?
Is this a shortcoming of the python API? would I have better luck
using the C++ one? anyone who can point me in the right direction?

Instead of Int, I'd like int, instead of Unexposed vector, I'd like
vector<string>, ...

(trying to build a tool for serialization, that needs the original
data type for diagnostic purposes and to generate code)

Thank you in advance,
Mark



More information about the cfe-dev mailing list