[cfe-dev] Access to the field of a structure

Alberto Barbaro barbaro.alberto at gmail.com
Sat Mar 7 15:58:04 PST 2015


Hi all,
i solved in this way. Probably it isn't the best one but i hope this help:

bool VisitDecl(Decl *D) {
    clang::RecordDecl::field_iterator jt;
    if (RecordDecl *var = dyn_cast<RecordDecl>(D)) {
      for(jt = var->field_begin(); jt != var->field_end();++jt) {
        std::cout << jt->getType().getAsString() << " " <<
jt->getNameAsString() << std::endl;
      }
    }
}

Any suggestion?

Thanks,
Alberto

2015-03-07 23:07 GMT+00:00 Alberto Barbaro <barbaro.alberto at gmail.com>:

> Hi All,
> i'm trying again to access to the fields of the struct but also after the
> first help i can't do it. I tried in this way but it isn't correct and i
> can't understand how to fix it:
>
> virtual bool VisitTypedefDecl (TypedefDecl  *d) {
>       cout << "typedef\n";
>       QualType qt = d->getUnderlyingType();
>       qt.dump();
>
>       for (clang::DeclContext::decl_iterator it = (qt).decls_begin(),
> declEnd = (qt).decls_end();it != declEnd; ++it) {
>     cout << "." << endl;
>       }
>
>       return true;
>     }
>
> Of course .decls_begins()/end() aren't available but i think cast to
> DeclContext() isn't the right solution.
>
> Any suggestion?
>
> Thanks,
> Alberto
>
> 2015-02-18 23:50 GMT+00:00 Alberto Barbaro <barbaro.alberto at gmail.com>:
>
>> Sean
>> Thanks I'll do as you have suggested.
>>
>> Alberto
>> On 18 Feb 2015 23:43, "Sean Silva" <chisophugis at gmail.com> wrote:
>>
>>> You will first want to get the RecordDecl
>>> through TypedefNameDecl::getUnderlyingType and then use the decls_begin/end
>>> iterators for iterating (or the decls() range in a range-for) (note these
>>> methods are inherited from DeclContext).
>>>
>>> -- Sean Silva
>>>
>>> On Sat, Feb 14, 2015 at 1:15 PM, Alberto Barbaro <
>>> barbaro.alberto at gmail.com> wrote:
>>>
>>>> Hi All,
>>>> in my .c file I have a struct like this:
>>>>
>>>> typedef struct var {
>>>>     int x;
>>>>     int y;
>>>> } point;
>>>>
>>>>
>>>> Overriding the VisitTypedefDecl method I can access to the structure
>>>> using a TypedefNameDecl variable. How can I access to the fields of the
>>>> struct?
>>>>
>>>> Thanks,
>>>> Alberto
>>>>
>>>> _______________________________________________
>>>> 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/20150307/b3908f7f/attachment.html>


More information about the cfe-dev mailing list