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

David Röthlisberger david at rothlis.net
Fri Jun 1 03:38:12 PDT 2012


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





More information about the cfe-dev mailing list