Assertion error when attempting to iterate over RecordDecl


A cross-post from cfe-users. I think there will be no luck
on that list.

This is wrong:

if(type->isRecordType())
{
const RecordType asRecordType = static_cast<const RecordType>(type);

Your Type is not necessarily a RecordType here; it might be a typedef for a record type or some other type sugar node. The right way to write this is:

if (const RecordType *asRecordType = type->getAs())

This is wrong:

if(type->isRecordType())
{
const RecordType asRecordType = static_cast<const RecordType>(type);

Your Type is not necessarily a RecordType here; it might be a typedef for a record type or some other type sugar node. The right way to write this is:

if (const RecordType *asRecordType = type->getAs())

Hey great, and thanks, is getAs always the thing to do versus simple casting?

Jeff,

Yes, Type::getAs is better than a normal cast. You get nullptr back if you try to cast to an invalid type.
From Type.h

05253 // Member-template getAs’.
05254 template const T *Type::getAs() const {
05255 ArrayType_cannot_be_used_with_getAs at;
05256 (void)at;
05257
05258 // If this is directly a T type, return it.
05259 if (const T *Ty = dyn_cast(this))
05260 return Ty;
05261
05262 // If the canonical form of this type isn’t the right kind, reject it.
05263 if (!isa(CanonicalType))
05264 return nullptr;
05265
05266 // If this is a typedef for the type, strip the typedef off without
05267 // losing all typedef information.
05268 return cast(getUnqualifiedDesugaredType());
05269 }