Get underlying type of QualType if it's TypedefType

Hi,

You can use getCanonicalType() to look through the typedefs, for example:

QualType SrcCanonType = SrcExpr()->getType().getCanonicalType(),
                DestCanonType = DestType.getCanonicalType();

if (const BuiltinType *Src = SrcCanonType->getAs<BuiltinType>())
  if (const BuiltinType *Dest = DestCanonType->getAs<BuiltinType>()) {
    if (Src.getKind() == Dest.getKind()) {
      // do something
    } else {
      // do something else
    }
}

Kirill

Kirill and Artem,

My original approach is dyn_cast the type pointer to TypedefType recursively since I need to take
nested typedef into consideration.

typedef foo FOO;
typedef FOO MyFoo;

MyFoo b = (MyFoo)(a);

The recursive call is kinda of like,

QualType getUnderlyingType(QualType QT) {

if (const TypedefType *TT = dyn_cast(QT)) {
TypedefNameDef TND = TT->getDecl();
return getUnderlyingType(TND->getUnderlyingType());
}

return QT;
}

Both of you suggested canonical type, I tried and it works.

QualType SrcCanonType = SrcExpr()->getType().getCanonicalType(),

const BuiltinType *Src = SrcCanonType->getAs()

I guess getCanonicalType() way is preferred?

Regards,
chenwj

Thanks. :slight_smile: