[cfe-dev] libclang: "Spelling" for built-in types?

Jacob Carlborg doob at me.com
Fri Jun 1 06:58:39 PDT 2012


On 2012-06-01 12:38, David Röthlisberger wrote:
> Hi,
>
> Using the libclang python bindings, how do I retrieve the name ("spelling") of a built-in type?
>
> I'll clarify with an example. For this source code "type.cpp":
>
>      struct S { };
>      S s;
>      int i;
>
> I obtain 2 Cursor objects "cs" and "ci" for the above variables "s" and "i":
>
>      import clang.cindex
>      index = clang.cindex.Index.create()
>      tu = index.parse("type.cpp")
>      cursors = tu.cursor.get_children()
>      c = cursors.next()
>      while c.spelling != "s":
>          c = cursors.next()
>      cs = c
>      ci = cursors.next()
>
> I can get the name of the user-defined type S:
>
>      cs.type.get_declaration().spelling
>
> which returns "S". But how do I get the type of "i"? Trying the same as the above:
>
>      ci.type.get_declaration().spelling
>
> returns None (python's null). The closest I've found is:
>
>      ci.type.kind.spelling
>
> which returns "Int". For other types it will return strings like "UChar", "Char16", "LongLong", or "Pointer". What I want is "int", "char", "int*", etc -- the way the type would be written in the source code.
>
> Is this information available and I just haven't found it?
>
> If not currently exposed by libclang, how hard would it be to add this information? And how would I go about it? (I don't want to reverse-engineer from the TypeKind.spelling†, and in some cases I couldn't if I wanted -- e.g. "Pointer" doesn't give me enough information.)
>
> Thanks very much!
> Dave.
>
> † clang_getTypeKindSpelling: https://github.com/llvm-mirror/clang/blob/25bd2793/tools/libclang/CXType.cpp#L380
> and its python binding: https://github.com/llvm-mirror/clang/blob/a63ef1f6/bindings/python/clang/cindex.py#L1218

I have a big switch statement, used on type.kind, for the built-in types.

If it's a pointer you can do the same as above but append a star "*". I 
don't know if there's a better way, I would prefer that "spelling" 
worked on built-in types as well.

-- 
/Jacob Carlborg




More information about the cfe-dev mailing list