[libtooling] Get readable string representation of type

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[libtooling] Get readable string representation of type

Simon Dardis via cfe-dev
I'm using libtooling to implement some C++ refactorings and I need a
string representation of types for use as template arguments in the
refactored code.  However, with certain complex types I'm having
difficulty getting a valid representation.

Suppose you've got this:

   std::vector<int> v;
   auto w = v;
   auto i = w.begin();

If you've got a DeclRefExpr* d for v, then
d->getDecl()->getType().getAsString() gives you


which is OK, but doing the same for w and i gives

    class std::__1::vector<int, class std::__1::allocator<int> >


    class std::__1::__wrap_iter<int *>

Neither of these is very human-readable, and clang itself will reject
source code containing these because of the __1.

I appreciate that because of typedefs and the like there's probably no
"best" representation of a type which the compiler has inferred
internally, but is there some way to get something which is at least
valid source code?

The CFE Internals manual appears to address this issue at
http://clang.llvm.org/docs/InternalsManual.html#canonical-types , but
what it says there  seems to be incorrect.  Specifically, it says "...
Type has a getAsPointerType() method that checks whether the type is
structurally a PointerType and, if so, returns the best one. If not, it
returns a null pointer."  There is no such method, and even if there was
I'd expect it to do something different.


Kenneth MacKenzie
cfe-dev mailing list
[hidden email]