<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Hi John,</div><div class=""><br class=""></div><div class="">IIUC you are creating a new RecordDecl, manually replacing the old one with the new one in its DeclContext, and then printing it.  As you seem to recognize this is the problematic line (recall this is from that DeclPrinter code I referenced the last time):</div><div class=""><br class=""></div><div class=""><a href="https://github.com/llvm-mirror/clang/blob/master/lib/AST/DeclPrinter.cpp#L394" class="">https://github.com/llvm-mirror/clang/blob/master/lib/AST/DeclPrinter.cpp#L410</a></div><div class=""><br class=""></div><div class="">You need the FieldDecl (which should be the very next Decl* in the DeclContext after the old RecordDecl *) to have an elaborated type whose owned tag decl is the new RecordDecl you have created (which will be `Decls[0]` in that code); if it is thus, and if the RecordDecl you created is marked as !isFreeStanding(), it should print as you expect, I think.</div><div class=""><br class=""></div><div class="">To create that type, use the ASTContext::getElaboratedType(…) method, which accepts the OwnedTagDecl as an arg.  (ASTContext manages the types to ensure there are not multiple copies of the same Type* floating around, IIUC.)</div><div class=""><br class=""></div><div class="">Then, I would probably also replace the existing FieldDecl with a newly Created one, in the same manner you replaced the RecordDecl *, only now Create it with the new type.</div><div class=""><br class=""></div><div class="">Hope that works — good luck,</div><div class=""><br class=""></div><div class="">Dave</div><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Feb 4, 2021, at 9:59 AM, John Leidel via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">I'm in the process of debugging (again) issues with anonymous struct<br class="">decls.  I have the following basic structures located in a C source<br class="">file that I'm attempting to transform.  I want to preserve the layout<br class="">of the structs.<br class=""><br class="">struct{<br class="">  int A;<br class="">}S;<br class=""><br class="">struct{<br class="">  int B;<br class="">  struct{<br class="">    int C;<br class="">  }T;<br class="">}R;<br class=""><br class="">When I use my AST visitors, I can pick up the RecordDecl for the<br class="">struct definitions and the VarDecl for the variable definitions, but<br class="">the transformed code is outputted as:<br class="">struct{<br class="">  int A;<br class="">};<br class="">struct (anonymous) S;<br class=""><br class="">I can view the RecordDecl as as TagDecl and save off a reference to it<br class="">if it is !isFreeStanding(), but how does one go about recombining the<br class="">declaration of "S" and the original TagDecl?<br class=""><br class="">For the VarDecl ("S" in this case), I can determine whether or not its<br class="">an "ElaboratedType", but I'm not sure how to utilize the saved<br class="">reference to the RecordDecl:<br class=""><br class="">if(isa<ElaboratedType>(SemaRef.Context.getBaseElementType(MyVarDecl))) {<br class="">   /// how do i recombine the VarDecl and the RecordDecl/TagDecl?<br class="">}<br class="">_______________________________________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a><br class="">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev<br class=""></div></div></blockquote></div><br class=""></div></body></html>