Skip to content

Commit d19041e

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 5467c88 commit d19041e

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
@@ -40,6 +40,9 @@ class DeclRewriter {
4040
std::string &IType, ProgramInfo &Info,
4141
ArrayBoundsRewriter &ABR);
4242

43+
// Called from the outside by FieldFinder.
44+
static void detectInlineStruct(Decl *D, SourceManager &SM);
45+
4346
private:
4447
static RecordDecl *LastRecordDecl;
4548
static std::map<Decl *, Decl *> VDToRDMap;
@@ -79,7 +82,6 @@ class DeclRewriter {
7982
bool isSingleDeclaration(DeclReplacement *N);
8083
bool areDeclarationsOnSameLine(DeclReplacement *N1, DeclReplacement *N2);
8184
SourceRange getNextCommaOrSemicolon(SourceLocation L);
82-
static void detectInlineStruct(Decl *D, SourceManager &SM);
8385
};
8486

8587
// Visits function declarations and adds entries with their new rewritten
@@ -129,6 +131,7 @@ class FieldFinder : public RecursiveASTVisitor<FieldFinder> {
129131
FieldFinder(GlobalVariableGroups &GVG) : GVG(GVG) {}
130132

131133
bool VisitFieldDecl(FieldDecl *FD);
134+
bool VisitRecordDecl(RecordDecl *RD);
132135

133136
static void gatherSameLineFields(GlobalVariableGroups &GVG, Decl *D);
134137

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

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

167167
virtual ~GlobalVariableGroups();
168168

169+
SourceManager &getSourceManager() { return SM; }
170+
169171
private:
170172
SourceManager &SM;
171173
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
@@ -521,15 +521,15 @@ void DeclRewriter::detectInlineStruct(Decl *D, SourceManager &SM) {
521521
RD->getBeginLoc().isValid()) {
522522
LastRecordDecl = RD;
523523
}
524-
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
524+
if (isa<VarDecl>(D) || isa<FieldDecl>(D)) {
525525
if (LastRecordDecl != nullptr) {
526526
auto LastRecordLocation = LastRecordDecl->getBeginLoc();
527-
auto Begin = VD->getBeginLoc();
528-
auto End = VD->getEndLoc();
527+
auto Begin = D->getBeginLoc();
528+
auto End = D->getEndLoc();
529529
bool IsInLineStruct = SM.isPointWithin(LastRecordLocation, Begin, End);
530530
if (IsInLineStruct) {
531-
VDToRDMap[VD] = LastRecordDecl;
532-
InlineVarDecls.insert(VD);
531+
VDToRDMap[D] = LastRecordDecl;
532+
InlineVarDecls.insert(D);
533533
}
534534
}
535535
}
@@ -859,7 +859,13 @@ bool FunctionDeclBuilder::inParamMultiDecl(const ParmVarDecl *PVD) {
859859
return false;
860860
}
861861

862+
bool FieldFinder::VisitRecordDecl(RecordDecl *RD) {
863+
DeclRewriter::detectInlineStruct(RD, GVG.getSourceManager());
864+
return true;
865+
}
866+
862867
bool FieldFinder::VisitFieldDecl(FieldDecl *FD) {
868+
DeclRewriter::detectInlineStruct(FD, GVG.getSourceManager());
863869
GVG.addGlobalDecl(FD);
864870
return true;
865871
}

0 commit comments

Comments
 (0)