@@ -235,7 +235,7 @@ void DeclRewriter::rewriteDecls(ASTContext &Context, ProgramInfo &Info,
235235 }
236236
237237 // Do the declaration rewriting
238- DeclRewriter DeclR (R, Context, GVG);
238+ DeclRewriter DeclR (R, Info, Context, GVG);
239239 DeclR.rewrite (RewriteThese);
240240
241241 for (auto Pair : RewriteThese)
@@ -278,7 +278,7 @@ void DeclRewriter::rewriteTypedefDecl(TypedefDeclReplacement *TDR,
278278// SourceLocations will be generated incorrectly if we rewrite it as a
279279// normal multidecl.
280280bool isInlineStruct (std::vector<Decl *> &InlineDecls) {
281- if (InlineDecls.size () >= 2 && AllTypes )
281+ if (InlineDecls.size () >= 2 )
282282 return isa<RecordDecl>(InlineDecls[0 ]) &&
283283 std::all_of (InlineDecls.begin () + 1 , InlineDecls.end (),
284284 [](Decl *D) { return isa<VarDecl>(D); });
@@ -306,9 +306,7 @@ void DeclRewriter::rewriteFieldOrVarDecl(DRType *N, RSet &ToRewrite) {
306306 } else if (!IsVisitedMultiDeclMember) {
307307 std::vector<Decl *> SameLineDecls;
308308 getDeclsOnSameLine (N, SameLineDecls);
309- if (isInlineStruct (SameLineDecls))
310- SameLineDecls.erase (SameLineDecls.begin ());
311- rewriteMultiDecl (N, ToRewrite, SameLineDecls, false );
309+ rewriteMultiDecl (N, ToRewrite, SameLineDecls, isInlineStruct (SameLineDecls));
312310 } else {
313311 // Anything that reaches this case should be a multi-declaration that has
314312 // already been rewritten.
@@ -372,7 +370,20 @@ void DeclRewriter::rewriteMultiDecl(DeclReplacement *N, RSet &ToRewrite,
372370 " no other decls?" );
373371 rewriteSourceRange (R, CharSourceRange::getCharRange (
374372 SameLineDecls[1 ]->getBeginLoc (), DL->getBeginLoc ()), " " );
375- // TODO: If the struct is unnamed, insert its assigned name.
373+ RecordDecl *RD = cast<RecordDecl>(DL);
374+ // If the record is unnamed, insert the name that we assigned it.
375+ if (RD->getName ().empty ()) {
376+ PersistentSourceLoc PSL = PersistentSourceLoc::mkPSL (RD, A);
377+ auto Iter = Info.AssignedRecordNames .find (PSL);
378+ if (Iter != Info.AssignedRecordNames .end ()) {
379+ // We hope that the token we are inserting after is the tag kind
380+ // keyword, e.g., `struct`. TODO: Verify this? And is that always the
381+ // correct place to insert the name?
382+ // TODO: Use a wrapper like rewriteSourceRange that reports failures
383+ // and tries harder to cope with macros.
384+ R.InsertTextAfterToken (DL->getBeginLoc (), " " + Iter->second );
385+ }
386+ }
376387 } else if (IsFirst) {
377388 // Rewriting the first declaration is easy. Nothing should change if its
378389 // type does not to be rewritten. When rewriting is required, it is
@@ -506,9 +517,7 @@ void DeclRewriter::detectInlineStruct(Decl *D, SourceManager &SM) {
506517 auto Begin = VD->getBeginLoc ();
507518 auto End = VD->getEndLoc ();
508519 bool IsInLineStruct = SM.isPointWithin (LastRecordLocation, Begin, End);
509- bool IsNamedInLineStruct =
510- IsInLineStruct && LastRecordDecl->getNameAsString () != " " ;
511- if (IsNamedInLineStruct) {
520+ if (IsInLineStruct) {
512521 VDToRDMap[VD] = LastRecordDecl;
513522 InlineVarDecls.insert (VD);
514523 }
0 commit comments