Skip to content

Commit ec195c0

Browse files
Insert assigned names into unnamed struct definitions. (Draft)
1 parent 4438a0e commit ec195c0

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

clang/include/clang/3C/DeclRewriter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ using namespace clang;
2626

2727
class DeclRewriter {
2828
public:
29-
DeclRewriter(Rewriter &R, ASTContext &A, GlobalVariableGroups &GP)
30-
: R(R), A(A), GP(GP) {}
29+
DeclRewriter(Rewriter &R, ProgramInfo &Info, ASTContext &A, GlobalVariableGroups &GP)
30+
: R(R), Info(Info), A(A), GP(GP) {}
3131

3232
// The publicly accessible interface for performing declaration rewriting.
3333
// All declarations for variables with checked types in the variable map of
@@ -44,6 +44,7 @@ class DeclRewriter {
4444
static std::map<Decl *, Decl *> VDToRDMap;
4545
static std::set<Decl *> InlineVarDecls;
4646
Rewriter &R;
47+
ProgramInfo &Info;
4748
ASTContext &A;
4849
GlobalVariableGroups &GP;
4950

clang/lib/3C/DeclRewriter.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
280280
bool 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

Comments
 (0)