[cfe-dev] Iterate over struct's fields
Hayrapetyan, Anahit via cfe-dev
cfe-dev at lists.llvm.org
Wed Feb 6 01:36:16 PST 2019
Hi Miklos,
Yes, I tried getUnderlyingType(). Here is the code I have:
void iterateTypeFields(const clang::Type* type)
{
clang::RecordDecl* recordDecl;
if (auto* typedefType = llvm::dyn_cast<clang::TypedefType>(type)) {
typedefType->getDecl()->getUnderlyingType()->dump();
iterateTypeFields(&*typedefType->getDecl()->getUnderlyingType());
} else if (auto* elaboratedType = llvm::dyn_cast<clang::ElaboratedType>(type)) {
elaboratedType->getNamedType()->dump();
iterateTypeFields(&*elaboratedType->getNamedType());
} else if (auto* recordType = llvm::dyn_cast<clang::RecordType>(type)) {
recordType->dump();
recordDecl = recordType->getDecl()->getDefinition();
} else if (type->isStructureType()) {
recordDecl = type->getAsStructureType()->getDecl();
} else {
return;
}
int i = 0;
for (auto it = recordDecl->field_begin(); it != recordDecl->field_end(); ++it) {
// Do stuff
}
}
When this code is ran on the example I posted, first I get TypedefType, then its' underlying type is ElaboratedType which getNamedType is the RecordType. So this function is called with TypedefType, then ElaboratedType then RecordType. However when I try to call field_begin function on RecordDecl obtained from RecordType it crashes in clang sources. That's why I'm assuming that I'm doing something wrong here.
Thanks!
________________________________
From: Miklos Vajna <vmiklos at vmiklos.hu>
Sent: Wednesday, February 6, 2019 9:46 AM
To: Hayrapetyan, Anahit
Cc: cfe-dev at lists.llvm.org
Subject: Re: [cfe-dev] Iterate over struct's fields
Hi,
On Tue, Feb 05, 2019 at 08:38:36PM +0000, "Hayrapetyan, Anahit via cfe-dev" <cfe-dev at lists.llvm.org> wrote:
> How can I get the real RecordDecl, the one that also has FieldDecls for struct?
Did you try getUnderlyingType()? See
<http://clang.llvm.org/doxygen/classclang_1_1TypedefNameDecl.html>.
Regards,
Miklos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190206/e04d4a07/attachment.html>
More information about the cfe-dev
mailing list