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