@@ -235,7 +235,7 @@ void DeclRewriter::rewriteDecls(ASTContext &Context, ProgramInfo &Info,
235
235
}
236
236
237
237
// Do the declaration rewriting
238
- DeclRewriter DeclR (R, Context, GVG);
238
+ DeclRewriter DeclR (R, Info, Context, GVG);
239
239
DeclR.rewrite (RewriteThese);
240
240
241
241
for (auto Pair : RewriteThese)
@@ -278,7 +278,7 @@ void DeclRewriter::rewriteTypedefDecl(TypedefDeclReplacement *TDR,
278
278
// SourceLocations will be generated incorrectly if we rewrite it as a
279
279
// normal multidecl.
280
280
bool isInlineStruct (std::vector<Decl *> &InlineDecls) {
281
- if (InlineDecls.size () >= 2 && AllTypes )
281
+ if (InlineDecls.size () >= 2 )
282
282
return isa<RecordDecl>(InlineDecls[0 ]) &&
283
283
std::all_of (InlineDecls.begin () + 1 , InlineDecls.end (),
284
284
[](Decl *D) { return isa<VarDecl>(D); });
@@ -306,9 +306,7 @@ void DeclRewriter::rewriteFieldOrVarDecl(DRType *N, RSet &ToRewrite) {
306
306
} else if (!IsVisitedMultiDeclMember) {
307
307
std::vector<Decl *> SameLineDecls;
308
308
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));
312
310
} else {
313
311
// Anything that reaches this case should be a multi-declaration that has
314
312
// already been rewritten.
@@ -372,7 +370,20 @@ void DeclRewriter::rewriteMultiDecl(DeclReplacement *N, RSet &ToRewrite,
372
370
" no other decls?" );
373
371
rewriteSourceRange (R, CharSourceRange::getCharRange (
374
372
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
+ }
376
387
} else if (IsFirst) {
377
388
// Rewriting the first declaration is easy. Nothing should change if its
378
389
// type does not to be rewritten. When rewriting is required, it is
@@ -506,9 +517,7 @@ void DeclRewriter::detectInlineStruct(Decl *D, SourceManager &SM) {
506
517
auto Begin = VD->getBeginLoc ();
507
518
auto End = VD->getEndLoc ();
508
519
bool IsInLineStruct = SM.isPointWithin (LastRecordLocation, Begin, End);
509
- bool IsNamedInLineStruct =
510
- IsInLineStruct && LastRecordDecl->getNameAsString () != " " ;
511
- if (IsNamedInLineStruct) {
520
+ if (IsInLineStruct) {
512
521
VDToRDMap[VD] = LastRecordDecl;
513
522
InlineVarDecls.insert (VD);
514
523
}
0 commit comments