[PATCH] Another patch for unnamed structure/union fields

I see that Pierre d'Herbemont has created a patch for unnamed inner records. At the same time I've been working on one as well. I'd like to submit it here too, at the suggestion of Eli Friedman.

This patch causes clang to add extra MemberExpr nodes to the AST when fields of inner unnamed records are accessed. I first tried to implement the necessary logic in CodeGen, but this proved to be overly complex: the code generator is really welded to the idea that each MemberExpr denotes a single GEP instruction in either a single struct or a single union. In my proposed patch, the Sema module expands access to an inner field into the correct number of MemberExpr nodes at AST construction time. This approach essentially treats unnamed structs and unions as syntactic sugar, which seems valid to me.

The CodePrinter logic is modified to deal with this. I've tested code generation on x86_64 and through lli, and it seems to work.

Pierre's patch selectively turns on the extension only when -std=gnu99 is specified. This is a good idea IMO, which I hadn't thought of. So I would suggest that our two patches be merged, because mine adds the code generation, while his has better parsing logic.

Comments would be appreciated. Thanks!

Patrick

clang-unnamed-union.patch (6.82 KB)

Hi Patrick!

I see that Pierre d'Herbemont has created a patch for unnamed inner records. At the same time I've been working on one as well. I'd like to submit it here too, at the suggestion of Eli Friedman.

Cool :slight_smile:

This patch causes clang to add extra MemberExpr nodes to the AST when fields of inner unnamed records are accessed. I first tried to implement the necessary logic in CodeGen, but this proved to be overly complex: the code generator is really welded to the idea that each MemberExpr denotes a single GEP instruction in either a single struct or a single union. In my proposed patch, the Sema module expands access to an inner field into the correct number of MemberExpr nodes at AST construction time. This approach essentially treats unnamed structs and unions as syntactic sugar, which seems valid to me.

Well, seems like a way cleaner way to go that what I tried to achieve...

The CodePrinter logic is modified to deal with this. I've tested code generation on x86_64 and through lli, and it seems to work.

Pierre's patch selectively turns on the extension only when -std=gnu99 is specified. This is a good idea IMO, which I hadn't thought of. So I would suggest that our two patches be merged, because mine adds the code generation, while his has better parsing logic.

Well, I'll try to rebase my work on yours. I think that I should port the conflicts between unnamed members check, along with the changes that allow the clang test suite to pass...

Pierre.

Well, I'll try to rebase my work on yours. I think that I should port the conflicts between unnamed members check, along with the changes that allow the clang test suite to pass...

That sounds great! Eli also suggested that implicitly created MemberExprs should have a null SourceLocation, since they didn't actually come from a specific location in the source. I think it might be good to just add an Implicit flag to MemberExprs as well in order to simplify some convoluted logic in the StmtPrinter.

Patrick