Clang AST: Modifying anonymous structs

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Clang AST: Modifying anonymous structs

shirley breuer via cfe-dev
I'm working on an external Clang tool that translates one c-based
language construct to another.  I have a series of recursive AST
functions that walk and modify elements as necessary.  I have an issue
with anonymous structures that reside within structs or unions.

Specifically, I have:
typedef struct test_case_t {
  struct {int value;} first[1];
  int second;
  int third[1];
} test_case_t;

That gets translated to:
struct test_case_t {
    struct {   // should be struct first
        int value;
    };
    struct first first[1];
    int second;
    int third[1];
};
typedef struct test_case_t test_case_t;

Note that the nested struct should be "struct first{ ... }".

My question is, using the FieldDecl for "first[1]", how do I derive
the defining struct type and modify the definition to be "struct
first{ ... }"?  I'm able to construct a QualType that is "struct
test_case_t::first", but I'm not sure how to modify the existing
struct definition.

Any thoughts?
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clang AST: Modifying anonymous structs

shirley breuer via cfe-dev
As a brief follow-up to my original question, why does Clang's AST
implicitly insert "(anonymous ..)" declaration names into an anonymous
struct definition when simply copying them to a new Decl?

EG:
struct {
  int in;
  int out;
} X;

gets translated to:
struct {
    int in;
    int out;
};
struct (anonymous ...) X;

On Fri, Dec 18, 2020 at 2:31 PM John Leidel <[hidden email]> wrote:

>
> I'm working on an external Clang tool that translates one c-based
> language construct to another.  I have a series of recursive AST
> functions that walk and modify elements as necessary.  I have an issue
> with anonymous structures that reside within structs or unions.
>
> Specifically, I have:
> typedef struct test_case_t {
>   struct {int value;} first[1];
>   int second;
>   int third[1];
> } test_case_t;
>
> That gets translated to:
> struct test_case_t {
>     struct {   // should be struct first
>         int value;
>     };
>     struct first first[1];
>     int second;
>     int third[1];
> };
> typedef struct test_case_t test_case_t;
>
> Note that the nested struct should be "struct first{ ... }".
>
> My question is, using the FieldDecl for "first[1]", how do I derive
> the defining struct type and modify the definition to be "struct
> first{ ... }"?  I'm able to construct a QualType that is "struct
> test_case_t::first", but I'm not sure how to modify the existing
> struct definition.
>
> Any thoughts?
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev