[cfe-dev] Clang AST: Modifying anonymous structs

John Leidel via cfe-dev cfe-dev at lists.llvm.org
Fri Dec 18 18:27:15 PST 2020


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 <john.leidel at gmail.com> 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?


More information about the cfe-dev mailing list