[cfe-dev] Assertion error when attempting to iterate over RecordDecl

Neil Semmel neil.semmel at gmail.com
Thu Mar 5 10:52:03 PST 2015


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<specific type>'.
05254 template <typename T> const T *Type::getAs() const {
05255   ArrayType_cannot_be_used_with_getAs<T> at;
05256   (void)at;
05257
05258   // If this is directly a T type, return it.
05259   if (const T *Ty = dyn_cast<T>(this))
05260     return Ty;
05261
05262   // If the canonical form of this type isn't the right kind, reject
it.
05263   if (!isa<T>(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<T>(getUnqualifiedDesugaredType());
05269 }

On Thu, Mar 5, 2015 at 5:09 AM, Jeff Waller <truthset at gmail.com> wrote:

>
> On Mar 4, 2015, at 6:41 PM, Richard Smith <richard at metafoo.co.uk> wrote:
>
> 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<RecordType>())
>
>
> Hey great, and thanks, is getAs always the thing to do versus simple
> casting?
>
>
> On Wed, Mar 4, 2015 at 3:15 PM, Jeff Waller <truthset at gmail.com> wrote:
>
>>
>> A cross-post from cfe-users. I think there will be no luck
>>
>> on that list.
>>
>> =======================
>>
>> I’m getting the failure:
>>
>> void clang::RecordDecl::LoadFieldsFromExternalStorage() const: Assertion `hasExternalLexicalStorage() && Source && "No external storage?"' failed.
>> when trying to iterate over a RecordDecl and not sure even what’s going on let along how to deal with it:
>>
>> I’m proceeding from the example inhttp://clang.llvm.org/docs/LibASTMatchersTutorial.html
>>
>> Here is some context:https://gist.github.com/waTeim/8757662c445ce3b4084f
>>
>> Thanks in advance for any help
>>
>>
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>
>>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150305/1c4d9117/attachment.html>


More information about the cfe-dev mailing list