Skip to content

Commit 8b5ebaa

Browse files
Minimal fix to detect inline structs nested in other structs so
multi-decl rewriting splits them rather than losing them. Fixes #531 except for a "declaration does not declare anything" compiler warning that will be addressed by de-nesting the inline structs.
1 parent 984a814 commit 8b5ebaa

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class DeclRewriter {
3939
std::string &IType, ProgramInfo &Info,
4040
ArrayBoundsRewriter &ABR);
4141

42+
// Called from the outside by FieldFinder.
43+
static void detectInlineStruct(Decl *D, SourceManager &SM);
44+
4245
private:
4346
static RecordDecl *LastRecordDecl;
4447
static std::map<Decl *, Decl *> VDToRDMap;
@@ -75,7 +78,6 @@ class DeclRewriter {
7578
void getDeclsOnSameLine(DeclReplacement *N, std::vector<Decl *> &Decls);
7679
bool isSingleDeclaration(DeclReplacement *N);
7780
SourceRange getNextCommaOrSemicolon(SourceLocation L);
78-
static void detectInlineStruct(Decl *D, SourceManager &SM);
7981
};
8082

8183
// Visits function declarations and adds entries with their new rewritten
@@ -126,6 +128,7 @@ class FieldFinder : public RecursiveASTVisitor<FieldFinder> {
126128
FieldFinder(GlobalVariableGroups &GVG) : GVG(GVG) {}
127129

128130
bool VisitFieldDecl(FieldDecl *FD);
131+
bool VisitRecordDecl(RecordDecl *RD);
129132

130133
static void gatherSameLineFields(GlobalVariableGroups &GVG, Decl *D);
131134

clang/include/clang/3C/RewriteUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ class GlobalVariableGroups {
132132

133133
virtual ~GlobalVariableGroups();
134134

135+
SourceManager &getSourceManager() { return SM; }
136+
135137
private:
136138
SourceManager &SM;
137139
std::map<Decl *, std::vector<Decl *> *> GlobVarGroups;

clang/lib/3C/DeclRewriter.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,15 +502,15 @@ void DeclRewriter::detectInlineStruct(Decl *D, SourceManager &SM) {
502502
RD->getBeginLoc().isValid()) {
503503
LastRecordDecl = RD;
504504
}
505-
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
505+
if (isa<VarDecl>(D) || isa<FieldDecl>(D)) {
506506
if (LastRecordDecl != nullptr) {
507507
auto LastRecordLocation = LastRecordDecl->getBeginLoc();
508-
auto Begin = VD->getBeginLoc();
509-
auto End = VD->getEndLoc();
508+
auto Begin = D->getBeginLoc();
509+
auto End = D->getEndLoc();
510510
bool IsInLineStruct = SM.isPointWithin(LastRecordLocation, Begin, End);
511511
if (IsInLineStruct) {
512-
VDToRDMap[VD] = LastRecordDecl;
513-
InlineVarDecls.insert(VD);
512+
VDToRDMap[D] = LastRecordDecl;
513+
InlineVarDecls.insert(D);
514514
}
515515
}
516516
}
@@ -890,7 +890,13 @@ bool FunctionDeclBuilder::inParamMultiDecl(const ParmVarDecl *PVD) {
890890
return false;
891891
}
892892

893+
bool FieldFinder::VisitRecordDecl(RecordDecl *RD) {
894+
DeclRewriter::detectInlineStruct(RD, GVG.getSourceManager());
895+
return true;
896+
}
897+
893898
bool FieldFinder::VisitFieldDecl(FieldDecl *FD) {
899+
DeclRewriter::detectInlineStruct(FD, GVG.getSourceManager());
894900
GVG.addGlobalDecl(FD);
895901
return true;
896902
}

0 commit comments

Comments
 (0)