Skip to content

Commit

Permalink
Minimal fix to detect inline structs nested in other structs so
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mattmccutchen-cci committed Sep 14, 2021
1 parent 9dc4968 commit fb819ef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
5 changes: 4 additions & 1 deletion clang/include/clang/3C/DeclRewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class DeclRewriter {
std::string &IType, ProgramInfo &Info,
ArrayBoundsRewriter &ABR);

// Called from the outside by FieldFinder.
static void detectInlineStruct(Decl *D, SourceManager &SM);

private:
static RecordDecl *LastRecordDecl;
static std::map<Decl *, Decl *> VDToRDMap;
Expand Down Expand Up @@ -75,7 +78,6 @@ class DeclRewriter {
void getDeclsOnSameLine(DeclReplacement *N, std::vector<Decl *> &Decls);
bool isSingleDeclaration(DeclReplacement *N);
SourceRange getNextCommaOrSemicolon(SourceLocation L);
static void detectInlineStruct(Decl *D, SourceManager &SM);
};

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

bool VisitFieldDecl(FieldDecl *FD);
bool VisitRecordDecl(RecordDecl *RD);

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

Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/3C/RewriteUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class GlobalVariableGroups {

virtual ~GlobalVariableGroups();

SourceManager &getSourceManager() { return SM; }

private:
SourceManager &SM;
std::map<Decl *, std::vector<Decl *> *> GlobVarGroups;
Expand Down
16 changes: 11 additions & 5 deletions clang/lib/3C/DeclRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,15 +499,15 @@ void DeclRewriter::detectInlineStruct(Decl *D, SourceManager &SM) {
RD->getBeginLoc().isValid()) {
LastRecordDecl = RD;
}
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
if (isa<VarDecl>(D) || isa<FieldDecl>(D)) {
if (LastRecordDecl != nullptr) {
auto LastRecordLocation = LastRecordDecl->getBeginLoc();
auto Begin = VD->getBeginLoc();
auto End = VD->getEndLoc();
auto Begin = D->getBeginLoc();
auto End = D->getEndLoc();
bool IsInLineStruct = SM.isPointWithin(LastRecordLocation, Begin, End);
if (IsInLineStruct) {
VDToRDMap[VD] = LastRecordDecl;
InlineVarDecls.insert(VD);
VDToRDMap[D] = LastRecordDecl;
InlineVarDecls.insert(D);
}
}
}
Expand Down Expand Up @@ -895,7 +895,13 @@ bool FunctionDeclBuilder::inParamMultiDecl(const ParmVarDecl *PVD) {
return false;
}

bool FieldFinder::VisitRecordDecl(RecordDecl *RD) {
DeclRewriter::detectInlineStruct(RD, GVG.getSourceManager());
return true;
}

bool FieldFinder::VisitFieldDecl(FieldDecl *FD) {
DeclRewriter::detectInlineStruct(FD, GVG.getSourceManager());
GVG.addGlobalDecl(FD);
return true;
}
Expand Down

0 comments on commit fb819ef

Please sign in to comment.