<div>On Tue Jan 28 2014 at 3:43:30 AM, Kevin Funk <<a href="mailto:krf@gmx.de">krf@gmx.de</a>> wrote:</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Am Sonntag, 26. Januar 2014, 13:00:24 schrieb Jacob Carlborg:<br>
> On 2014-01-25 22:03, Kevin Funk wrote:<br>
> > That doesn't work for me.<br>
> ><br>
> > For testing we have a small wrapper binary that basically creates a<br>
> > CXTranslationUnit and then traverses through the AST via<br>
> > clang_visitChildren().<br>
> ><br>
> > During the call to 'visit' it does the following (amongst other things):<br>
> > - auto location = clang_getCursorLocation(<u></u>cursor)<br>
> > - auto type = clang_getCursorType(cursor)<br>
> > - auto typeString = clang_getTypeSpelling(type)<br>
> > - now: auto canonicalTypeString =<br>
> ><br>
> > clang_getTypeSpelling(clang_<u></u>getCanonicalType(type))<br>
> ><br>
> > It outputs all the values of those variabes to stdout, and when I pass<br>
> > "auto i = 5;" to it I get:<br>
> ><br>
> > """<br>
> > decl: "auto (canonical type: auto) i " of kind VarDecl (9) in<br>
> > stdin.cpp@1:6<br>
> ><br>
> > "int (canonical type: int) " of kind IntegerLiteral (106) in<br>
> > stdin.cpp@1:10<br>
> ><br>
> > """<br>
> ><br>
> > So the 'canonical type' of VarDecl still resolves to 'auto', instead of<br>
> > 'int'. Sorry, if we're doing something completely wrong, but I don't seem<br>
> > to get this working as you suggest.<br>
><br>
> Ok, I see, you're using clang_getTypeSpelling. I created my tool before<br>
> clang_getTypeSpelling was available. For aggregates, typedefs and a<br>
> couple of other types I'm using the following code[1]:<br>
><br>
> auto cursor = clang_getTypeDeclaration(type)<u></u>;<br>
> auto str = clang_getCursorSpelling(<u></u>cursor);<br>
><br>
> For basic types like "int", "char" and so on, the above will return an<br>
> empty string. For those types I use a big switch statement on the type<br>
> kind and just returns a string representation [2].<br>
><br>
> [1]<br>
> <a href="https://github.com/jacob-carlborg/dstep/blob/master/dstep/translator/Type.d#" target="_blank">https://github.com/jacob-<u></u>carlborg/dstep/blob/master/<u></u>dstep/translator/Type.d#</a><br>
> L43<br>
><br>
> [2]<br>
> <a href="https://github.com/jacob-carlborg/dstep/blob/master/dstep/translator/Type.d#" target="_blank">https://github.com/jacob-<u></u>carlborg/dstep/blob/master/<u></u>dstep/translator/Type.d#</a><br>
> L248<br>
<br>
Unfortunately, your source code doesn't help me, nor can I get it to work<br>
using any combination of getTypeDeclaration, getCursorSpelling, and the ones I<br>
referred to earlier...<br>
<br>
I wonder if there's some bug in Clang's type printers I'm experiencing here.<br>
<br>
Let's do some testing:<br>
Test file: test.cpp, containing 'auto i = 5;'<br>
<br>
$ clang -cc1 -std=c++11 -ast-dump test.cpp<br>
(...)<br>
`-VarDecl 0x1c2e5b0 <test.cpp:1:1, col:10> i 'int':'int'<br>
`-IntegerLiteral 0x1c2e608 <col:10> 'int' 5<br>
<br>
=> What I'd expect! ('auto' => 'int')<br>
<br>
Next try: My clang-standalone-parser (basically emulating 'clang -cc1 -dump')<br>
but using libclang only [1]:<br>
<br>
$ ./clang-standalone-parser -std=c++11 test.cpp<br>
test.cpp:1:6 (5, 0-10) kind: VarDecl type: auto display name: i (...)<br>
test.cpp:1:10 (9, 9-10) kind: IntegerLiteral type: int<br>
<br>
=> Not what I'd expect, 'auto' is not deduced<br>
<br>
And the odd part here is: Breaking on the symbol<br>
'clang::AutoType::<u></u>getDeducedType()' for both clang and clang-standalone-parser<br>
shows that *both* versions actually call that function when trying to find a<br>
string representation for the type 'auto'. But with clang-standalone-parser,<br>
'clang::AutoType::<u></u>getDeducedType()' always returns an null QualType.<br>
<br>
Can someone make any sense out of this? Bug in Clang/LLVM? Note that the<br>
backtrace towards the call of getDeducedType is slightly different for the two<br>
versions (see attached file). Maybe this is the reason?<br></blockquote><div><br></div><div>I would expect that clang's dumper is using VarDecl->getType() and your standalone tool is using VarDecl->getTypeSourceInfo(). The former produces the type of the variable (which is 'int'); the latter produces the type-as-written (which is 'auto').</div>
<div><br></div><div>FWIW, we've been considering changing this for variables, but even if we did, the problem would persist for functions with 'auto' return types.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Any help greatly appreciated!<br>
<br>
Greets<br>
<br>
[1] <a href="http://quickgit.kde.org/?p=kdev-clang.git&a=blob&h=dbad9e8942cc5e20fd005677fc32a5a0b54977ae&hb=1fa9eca7a9a58404c3ef687fcfa63800d459cff5&f=tests%2Fclang-standalone-parser.c" target="_blank">http://quickgit.kde.org/?p=<u></u>kdev-clang.git&a=blob&h=<u></u>dbad9e8942cc5e20fd005677fc32a5<u></u>a0b54977ae&hb=<u></u>1fa9eca7a9a58404c3ef687fcfa638<u></u>00d459cff5&f=tests%2Fclang-<u></u>standalone-parser.c</a><br>
<br>
Compiling is simple:<br>
$ clang clang-standalone-parser.c -I/path/to/llvm/include -L/path/to/llvm/lib<br>
-lclang -Wl,-rpath,'/path/to/llvm/lib' -o clang-standalone-parser<br>
<br>
--<br>
Kevin Funk__________________________<u></u>_____________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/cfe-dev</a><br>
</blockquote>