I'm trying to get the name of method parameter and variable types, but
the name returned by clang_getTypeSpelling contains part of the type as
used, like "const", "&", etc.
Is there any way to use libClang's API to get just the name of the type,
without any of these additions? I'm currently removing these from the
name manually, but it seems superfluous when Clang already has it.
Hi Sam,
here's one way which might work.
First, convert the CXType to a QualType with 'GetQualType'.
Combining 'getTypePtr'
https://github.com/llvm-mirror/clang/blob/master/include/clang/AST/Type.h#L653
and 'getPointeeOrArrayElementType'
https://github.com/llvm-mirror/clang/blob/master/include/clang/AST/Type.h#L1946
should provide the unqualified pointee type.
Probably, you also want to ignore implicit casts
https://github.com/llvm-mirror/clang/blob/master/include/clang/AST/Expr.h#L727 .
The QualType can be converted back to a CXType with 'MakeCXType'
https://github.com/trolldbois/libclang/blob/master/CXType.cpp#L98 .
-Alex
Good point. I missed that you’re using the C interface but it seems like similar functionality is available there:
clang_getPointeeType
https://github.com/llvm-mirror/clang/blob/master/include/clang-c/Index.h#L3437
clang_getElementType
https://github.com/llvm-mirror/clang/blob/master/include/clang-c/Index.h#L3513
Though, I couldn’t find functionality to remove the qualifiers.
-Alex
You have to get somewhat creative for that: In particular:
To get the name of a parameter you want to use clang_getCursorSpelling. To get the name of the type for the tool [1] that I've built I'm using clang_getCursorType and then a big switch [2] on the type kind, similar to what Jonathan Müller suggested.
[1] GitHub - jacob-carlborg/dstep: A tool for converting C and Objective-C headers to D modules
[2] https://github.com/jacob-carlborg/dstep/blob/master/dstep/translator/Type.d#L27