Skip to content

Commit 4bf6ff9

Browse files
Insert assigned names into unnamed struct definitions. (Draft)
1 parent dcf9da0 commit 4bf6ff9

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
@@ -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.
278278
bool 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

Comments
 (0)