[cfe-dev] Iterate over struct's fields

Hayrapetyan, Anahit via cfe-dev cfe-dev at lists.llvm.org
Tue Feb 5 12:38:36 PST 2019


Hi,

I have following C code

    typedef struct
    {
       unsigned int    speed;
       int             len;
       snake_segment_t body[100];
    } snake_t;

    void f(snake_t snake) {}

And I want to iterate over function `f`'s arguments and in case if they are of struct type (like here), over fields of the struct too.

In Clang AST the structure looks like this:

    |-RecordDecl 0x5611f85ce6c0 <line:45:9, line:52:1> line:45:9 struct definition
    | |-FieldDecl 0x5611f85ce788 <line:47:4, col:20> col:20 referenced speed 'unsigned int'
    | |-FieldDecl 0x5611f85ce860 <line:50:4, col:20> col:20 referenced len 'int'
    | `-FieldDecl 0x5611f85ce9d8 <line:51:4, col:28> col:20 referenced body 'snake_segment_t [100]'

    |-TypedefDecl 0x5611f85cea70 <line:45:1, line:52:3> col:3 referenced snake_t 'struct snake_t':'snake_t'
    | `-ElaboratedType 0x5611f85cea20 'struct snake_t' sugar
    |   `-RecordType 0x5611f85ce750 'snake_t'
    |     `-Record 0x5611f85ce6c0 ''


 First I find FunctionDecl for f, then iterate over its' ParamDecls to find all arguments. But when I retrieve Decl from ParamDecl->getType() to iterate over struct fields, I get the TypedefDecl (in AST). How can I get the real RecordDecl, the one that also has FieldDecls for struct?

Best,
Anahit.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190205/de828e01/attachment.html>


More information about the cfe-dev mailing list