Skip to content

Commit fb819ef

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 9dc4968 commit fb819ef

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
@@ -130,6 +130,8 @@ class GlobalVariableGroups {
130130

131131
virtual ~GlobalVariableGroups();
132132

133+
SourceManager &getSourceManager() { return SM; }
134+
133135
private:
134136
SourceManager &SM;
135137
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
@@ -499,15 +499,15 @@ void DeclRewriter::detectInlineStruct(Decl *D, SourceManager &SM) {
499499
RD->getBeginLoc().isValid()) {
500500
LastRecordDecl = RD;
501501
}
502-
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
502+
if (isa<VarDecl>(D) || isa<FieldDecl>(D)) {
503503
if (LastRecordDecl != nullptr) {
504504
auto LastRecordLocation = LastRecordDecl->getBeginLoc();
505-
auto Begin = VD->getBeginLoc();
506-
auto End = VD->getEndLoc();
505+
auto Begin = D->getBeginLoc();
506+
auto End = D->getEndLoc();
507507
bool IsInLineStruct = SM.isPointWithin(LastRecordLocation, Begin, End);
508508
if (IsInLineStruct) {
509-
VDToRDMap[VD] = LastRecordDecl;
510-
InlineVarDecls.insert(VD);
509+
VDToRDMap[D] = LastRecordDecl;
510+
InlineVarDecls.insert(D);
511511
}
512512
}
513513
}
@@ -895,7 +895,13 @@ bool FunctionDeclBuilder::inParamMultiDecl(const ParmVarDecl *PVD) {
895895
return false;
896896
}
897897

898+
bool FieldFinder::VisitRecordDecl(RecordDecl *RD) {
899+
DeclRewriter::detectInlineStruct(RD, GVG.getSourceManager());
900+
return true;
901+
}
902+
898903
bool FieldFinder::VisitFieldDecl(FieldDecl *FD) {
904+
DeclRewriter::detectInlineStruct(FD, GVG.getSourceManager());
899905
GVG.addGlobalDecl(FD);
900906
return true;
901907
}

0 commit comments

Comments
 (0)