[cfe-dev] [libtooling] Get readable string representation of type

Kenneth MacKenzie via cfe-dev cfe-dev at lists.llvm.org
Fri Jun 30 00:18:37 PDT 2017


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

    std::vector<int>

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

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

and

    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.

Thanks,

Kenneth MacKenzie



More information about the cfe-dev mailing list