Printing QualType with Dependencies resolved

Hi, I am trying to get a QualType to print properly, which works fine until there is a dependent type.


template<class T>
struct Foo {
    T data;
    T& data_ref;

// ...

PrintingPolicy policy( context->getLangOpts() );
policy.SuppressInlineNamespace = false;
policy.PrintCanonicalTypes = true;
policy.PolishForDeclaration = true;
policy.FullyQualifiedName = true;
policy.PrintInjectedClassNameWithArguments = true;

FieldDecl* data = ....;
QualType qt = data->getType();

std::string type = qt.getAsString( policy ); // results in "type-parameter-0-0" instead of "T"

FieldDecl* data_ref = ...;
QualType qt_ref = data_ref->getType();

std::string ref_type = qt_ref.getAsString(policy); // results in "type-parameter-0-0 &" instead of "T &"

Printing out the names with a printing policy results in type-parameter-0-0 instead of the actual name of the template variable.

How can I get these “placeholders” (?) replaced with their proper names?

The logic for printing type information lives in clang/lib/AST/TypePrinter.cpp and I believe you’d be getting into TypePrinter::printTemplateTypeParmBefore() to eventually get that type-parameter-0-0 printed. That seems to only happen if we can’t determine the identifier for the template type parameter. That said, I’m not certain why we’re not able to find the identifier for your case. I think you may have to dig into that a bit more to see what’s going on.