[cfe-dev] API for auto type deduction in libclang

Jacob Carlborg doob at me.com
Sun Jan 26 04:00:24 PST 2014


On 2014-01-25 22:03, Kevin Funk wrote:

> That doesn't work for me.
>
> For testing we have a small wrapper binary that basically creates a
> CXTranslationUnit and then traverses through the AST via
> clang_visitChildren().
>
> During the call to 'visit' it does the following (amongst other things):
> - auto location = clang_getCursorLocation(cursor)
> - auto type = clang_getCursorType(cursor)
> - auto typeString = clang_getTypeSpelling(type)
> - now: auto canonicalTypeString =
>      clang_getTypeSpelling(clang_getCanonicalType(type))
>
> It outputs all the values of those variabes to stdout, and when I pass "auto i
> = 5;" to it I get:
>
> """
> decl: "auto (canonical type: auto) i " of kind VarDecl (9) in stdin.cpp at 1:6
>    "int (canonical type: int) " of kind IntegerLiteral (106) in stdin.cpp at 1:10
> """
>
> So the 'canonical type' of VarDecl still resolves to 'auto', instead of 'int'.
> Sorry, if we're doing something completely wrong, but I don't seem to get this
> working as you suggest.

Ok, I see, you're using clang_getTypeSpelling. I created my tool before 
clang_getTypeSpelling was available. For aggregates, typedefs and a 
couple of other types I'm using the following code[1]:

auto cursor = clang_getTypeDeclaration(type);
auto str = clang_getCursorSpelling(cursor);

For basic types like "int", "char" and so on, the above will return an 
empty string. For those types I use a big switch statement on the type 
kind and just returns a string representation [2].

[1] 
https://github.com/jacob-carlborg/dstep/blob/master/dstep/translator/Type.d#L43

[2] 
https://github.com/jacob-carlborg/dstep/blob/master/dstep/translator/Type.d#L248

-- 
/Jacob Carlborg




More information about the cfe-dev mailing list