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

Sean Silva chisophugis at gmail.com
Mon Mar 9 14:28:43 PDT 2015


Other than just overriding VisitRecordDecl (instead of VisitDecl) what
you've done seems to make sense. Sorry about the confusion; I thought for
some reason you already had a reference to the TypedefDecl and didn't want
to do another AST visitation to find the struct.

-- Sean Silva

On Sat, Mar 7, 2015 at 3:58 PM, Alberto Barbaro <barbaro.alberto at gmail.com>
wrote:

> 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/20150309/afdb17c9/attachment.html>


More information about the cfe-dev mailing list