@@ -233,7 +233,7 @@ void DeclRewriter::rewriteDecls(ASTContext &Context, ProgramInfo &Info,
233233 }
234234
235235 // Do the declaration rewriting
236- DeclRewriter DeclR (R, Context, GVG);
236+ DeclRewriter DeclR (R, Info, Context, GVG);
237237 DeclR.rewrite (RewriteThese);
238238
239239 for (auto Pair : RewriteThese)
@@ -276,7 +276,7 @@ void DeclRewriter::rewriteTypedefDecl(TypedefDeclReplacement *TDR,
276276// SourceLocations will be generated incorrectly if we rewrite it as a
277277// normal multidecl.
278278bool isInlineStruct (std::vector<Decl *> &InlineDecls) {
279- if (InlineDecls.size () >= 2 && _3COpts. AllTypes )
279+ if (InlineDecls.size () >= 2 )
280280 return isa<RecordDecl>(InlineDecls[0 ]) &&
281281 std::all_of (InlineDecls.begin () + 1 , InlineDecls.end (),
282282 [](Decl *D) { return isa<VarDecl>(D); });
@@ -304,9 +304,7 @@ void DeclRewriter::rewriteFieldOrVarDecl(DRType *N, RSet &ToRewrite) {
304304 } else if (!IsVisitedMultiDeclMember) {
305305 std::vector<Decl *> SameLineDecls;
306306 getDeclsOnSameLine (N, SameLineDecls);
307- if (isInlineStruct (SameLineDecls))
308- SameLineDecls.erase (SameLineDecls.begin ());
309- rewriteMultiDecl (N, ToRewrite, SameLineDecls, false );
307+ rewriteMultiDecl (N, ToRewrite, SameLineDecls, isInlineStruct (SameLineDecls));
310308 } else {
311309 // Anything that reaches this case should be a multi-declaration that has
312310 // already been rewritten.
@@ -369,7 +367,20 @@ void DeclRewriter::rewriteMultiDecl(DeclReplacement *N, RSet &ToRewrite,
369367 " no other decls?" );
370368 rewriteSourceRange (R, CharSourceRange::getCharRange (
371369 SameLineDecls[1 ]->getBeginLoc (), DL->getBeginLoc ()), " " );
372- // TODO: If the struct is unnamed, insert its assigned name.
370+ RecordDecl *RD = cast<RecordDecl>(DL);
371+ // If the record is unnamed, insert the name that we assigned it.
372+ if (RD->getName ().empty ()) {
373+ PersistentSourceLoc PSL = PersistentSourceLoc::mkPSL (RD, A);
374+ auto Iter = Info.AssignedRecordNames .find (PSL);
375+ if (Iter != Info.AssignedRecordNames .end ()) {
376+ // We hope that the token we are inserting after is the tag kind
377+ // keyword, e.g., `struct`. TODO: Verify this? And is that always the
378+ // correct place to insert the name?
379+ // TODO: Use a wrapper like rewriteSourceRange that reports failures
380+ // and tries harder to cope with macros.
381+ R.InsertTextAfterToken (DL->getBeginLoc (), " " + Iter->second );
382+ }
383+ }
373384 } else if (IsFirst) {
374385 // Rewriting the first declaration is easy. Nothing should change if its
375386 // type does not to be rewritten. When rewriting is required, it is
@@ -503,9 +514,7 @@ void DeclRewriter::detectInlineStruct(Decl *D, SourceManager &SM) {
503514 auto Begin = VD->getBeginLoc ();
504515 auto End = VD->getEndLoc ();
505516 bool IsInLineStruct = SM.isPointWithin (LastRecordLocation, Begin, End);
506- bool IsNamedInLineStruct =
507- IsInLineStruct && LastRecordDecl->getNameAsString () != " " ;
508- if (IsNamedInLineStruct) {
517+ if (IsInLineStruct) {
509518 VDToRDMap[VD] = LastRecordDecl;
510519 InlineVarDecls.insert (VD);
511520 }
0 commit comments