[cfe-dev] How to traverse all children FieldDecl from parent CXXRecordDecl

Lu Niu qqibrow at gmail.com
Tue May 26 10:07:01 PDT 2015


Hi,

Thank u for your answers! I have a new question here. Suppose I have a
struct dump like below:

typedef struct {
    int a;
    double b;
} Foo;

|-CXXRecordDecl 0x285db60 </home/qqibrow/test/main.cpp:5:9, line:8:1>
struct definition
| |-FieldDecl 0x285dc80 <line:6:5, col:9> a 'int'
| `-FieldDecl 0x285dce0 <line:7:5, col:12> b 'double'
|-TypedefDecl 0x285dd80 <line:5:1, line:8:3> Foo 'struct Foo':'Foo'

Is there a way to get access to that TypedefDecl of the struct from
the VisitCXXRecordDecl
method? For example:

bool VisitCXXRecordDecl(CXXRecordDecl *Declaration) {
      // 1. get name of the struct if possible from its TypedefDecl
      // 2. get all fields of the struct. Thanks to your previous answers,
I know how to do it now.

}

Best,
Lu


On Fri, May 22, 2015 at 11:05 PM, Manasij Mukherjee <manasij7479 at gmail.com>
wrote:

> Hi,
> As a CXXRecordDecl is a DeclContext, you could simply use the iterators
> you get by calling
> decls_begin and decls_end.
> For more sophisticated way, you could implement some visitor and
> VisitDeclContext from there.
>
> Manasij Mukherjee
>
> On Fri, May 22, 2015 at 10:24 PM, Lu Niu <qqibrow at gmail.com> wrote:
>
>> Hi,
>>
>> I am new to clang AST and try to parse a struct like below:
>>
>> struct Foo {
>>     int a;
>>     double b;
>> };
>>
>>
>> the ast-dump result is like:
>>
>> CXXRecordDecl 0x202c768 </home/lniu/ClangCheck/main.cpp:5:1, line:8:1>
>> line:5:8 struct Foo definition
>> |-CXXRecordDecl 0x202c880 <col:1, col:8> col:8 implicit struct Foo
>> |-FieldDecl 0x202c920 <line:6:5, col:9> col:9 a 'int'
>> `-FieldDecl 0x202c978 <line:7:5, col:12> col:12 b 'double'
>>
>> My question is How to traverse all children FieldDecl from parent
>> CXXRecordDecl?
>>
>> I follow the tutorial here
>> http://clang.llvm.org/docs/RAVFrontendAction.html
>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__clang.llvm.org_docs_RAVFrontendAction.html&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=CnzuN65ENJ1H9py9XLiRvC_UQz6u3oG6GUNn7_wosSM&m=TqPuWCZLnY-IO9LafQXkcSpIk762vbbWxET56jjf77Q&s=9gYIUUh9eBetgoGJVhAy58A1YT1jXy3LmcfhmQEMiew&e=>
>> and try to solve this inside bool VisitCXXRecordDecl(CXXRecordDecl
>> *Declaration) from a RecursiveASTVisitor<FindNamedClassVisitor>. I am
>> expecting pseudo code like below but couldn't find the api.
>>
>> bool VisitCXXRecordDecl(CXXRecordDecl *Declaration) {
>>
>> for(ChildrenDecl decl : Declaration.getChildren()) {
>> // do sth here
>> }
>>
>> }
>>
>> I don't know whether I am on the right path. Any idea is welcome. Thank
>> you!
>>
>> Best,
>> Lu
>>
>>
>> _______________________________________________
>> 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/20150526/ecf1207f/attachment.html>


More information about the cfe-dev mailing list