forked from vgvassilev/creduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimplifyStructUnionDecl.cpp
241 lines (190 loc) · 6.28 KB
/
SimplifyStructUnionDecl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2012, 2013, 2014 The University of Utah
// All rights reserved.
//
// This file is distributed under the University of Illinois Open Source
// License. See the file COPYING for details.
//
//===----------------------------------------------------------------------===//
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "SimplifyStructUnionDecl.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/SourceManager.h"
#include "TransformationManager.h"
using namespace clang;
using namespace llvm;
static const char *DescriptionMsg =
"This pass combines the declaration of global vars declared \
as struct/union with the corresponding struct/union \
declaration. \n";
static RegisterTransformation<SimplifyStructUnionDecl>
Trans("simplify-struct-union-decl", DescriptionMsg);
class SimplifyStructUnionDeclVisitor : public
RecursiveASTVisitor<SimplifyStructUnionDeclVisitor> {
public:
explicit SimplifyStructUnionDeclVisitor(SimplifyStructUnionDecl *Instance)
: ConsumerInstance(Instance)
{ }
bool VisitFunctionDecl(FunctionDecl *FD);
bool VisitVarDecl(VarDecl *VD);
bool VisitFieldDecl(FieldDecl *FD);
private:
SimplifyStructUnionDecl *ConsumerInstance;
};
bool SimplifyStructUnionDeclVisitor::VisitFunctionDecl(FunctionDecl *FD)
{
const Type *T = FD->getResultType().getTypePtr();
return ConsumerInstance->handleOneDeclarator(T);
}
bool SimplifyStructUnionDeclVisitor::VisitVarDecl(VarDecl *VD)
{
if (ConsumerInstance->CombinedVars.count(VD))
return true;
const Type *T = VD->getType().getTypePtr();
return ConsumerInstance->handleOneDeclarator(T);
}
bool SimplifyStructUnionDeclVisitor::VisitFieldDecl(FieldDecl *FD)
{
const Type *T = FD->getType().getTypePtr();
return ConsumerInstance->handleOneDeclarator(T);
}
void SimplifyStructUnionDecl::Initialize(ASTContext &context)
{
Transformation::Initialize(context);
AnalysisVisitor = new SimplifyStructUnionDeclVisitor(this);
}
bool SimplifyStructUnionDecl::HandleTopLevelDecl(DeclGroupRef DGR)
{
DeclGroupRef::iterator DI = DGR.begin();
const RecordDecl *RD = dyn_cast<RecordDecl>(*DI);
if (RD) {
addOneRecordDecl(RD, DGR);
return true;
}
VarDecl *VD = dyn_cast<VarDecl>(*DI);
if (!VD)
return true;
const Type *T = VD->getType().getTypePtr();
RD = getBaseRecordDecl(T);
if (!RD)
return true;
const Decl *CanonicalD = RD->getCanonicalDecl();
void *DGRPointer = RecordDeclToDeclGroup[CanonicalD];
if (!DGRPointer)
return true;
ValidInstanceNum++;
if (ValidInstanceNum != TransformationCounter)
return true;
TheRecordDecl = dyn_cast<RecordDecl>(CanonicalD);
TheDeclGroupRefs.push_back(DGRPointer);
TheDeclGroupRefs.push_back(DGR.getAsOpaquePtr());
for (DeclGroupRef::iterator I = DGR.begin(), E = DGR.end(); I != E; ++I) {
VarDecl *VD = dyn_cast<VarDecl>(*I);
TransAssert(VD && "Bad VarDecl!");
CombinedVars.insert(VD);
}
DeclGroupRef DefDGR = DeclGroupRef::getFromOpaquePtr(DGRPointer);
for (DeclGroupRef::iterator I = DefDGR.begin(),
E = DefDGR.end(); I != E; ++I) {
VarDecl *VD = dyn_cast<VarDecl>(*I);
if (VD)
CombinedVars.insert(VD);
}
return true;
}
void SimplifyStructUnionDecl::HandleTranslationUnit(ASTContext &Ctx)
{
if (QueryInstanceOnly) {
return;
}
if (TransformationCounter > ValidInstanceNum) {
TransError = TransMaxInstanceError;
return;
}
Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
TransAssert(AnalysisVisitor && "NULL AnalysisVisitor!");
TransAssert(TheRecordDecl && "NULL RecordDecl!");
AnalysisVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
doCombination();
if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
TransError = TransInternalError;
}
void SimplifyStructUnionDecl::doCombination(void)
{
TransAssert(TheDeclGroupRefs.size() == 2);
void *P2 = TheDeclGroupRefs.pop_back_val();
void *P1 = TheDeclGroupRefs.pop_back_val();
DeclGroupRef FirstDGR = DeclGroupRef::getFromOpaquePtr(P1);
DeclGroupRef SecondDGR = DeclGroupRef::getFromOpaquePtr(P2);
SourceLocation EndLoc =
RewriteHelper->getDeclGroupRefEndLoc(FirstDGR);
std::string DStr;
RewriteHelper->getDeclGroupStrAndRemove(SecondDGR, DStr);
// it's a single RecordDecl
if (FirstDGR.isSingleDecl())
TheRewriter.InsertText(EndLoc, DStr, /*InsertAfter=*/false);
else
TheRewriter.InsertText(EndLoc, ", " + DStr, /*InsertAfter=*/false);
if (isSafeToRemoveName()) {
SourceLocation NameLocStart = TheRecordDecl->getLocation();
std::string Name = TheRecordDecl->getNameAsString();
TheRewriter.RemoveText(NameLocStart, Name.size());
}
}
bool SimplifyStructUnionDecl::isSafeToRemoveName(void)
{
if (!SafeToRemoveName)
return false;
const RecordDecl *RD =
dyn_cast<RecordDecl>(TheRecordDecl->getCanonicalDecl());
RecordDecl::redecl_iterator I = RD->redecls_begin();
RecordDecl::redecl_iterator E = RD->redecls_end();
++I;
return (I == E);
}
void SimplifyStructUnionDecl::addOneRecordDecl(const RecordDecl *RD,
DeclGroupRef DGR)
{
const Decl *CanonicalD = RD->getCanonicalDecl();
const RecordDecl *RDDef = RD->getDefinition();
if (!RDDef || (RD != RDDef))
return;
if (!RecordDeclToDeclGroup[CanonicalD])
RecordDeclToDeclGroup[CanonicalD] = (DGR.getAsOpaquePtr());
}
const RecordDecl *SimplifyStructUnionDecl::getBaseRecordDecl(const Type *T)
{
const ArrayType *ArrayTy = dyn_cast<ArrayType>(T);
if (ArrayTy)
T = getArrayBaseElemType(ArrayTy);
if (T->isPointerType())
T = getBasePointerElemType(T);
const RecordType *RT = NULL;
if (T->isUnionType())
RT = T->getAsUnionType();
else if (T->isStructureType())
RT = T->getAsStructureType();
else
return NULL;
return RT->getDecl();
}
bool SimplifyStructUnionDecl::handleOneDeclarator(const Type *Ty)
{
const RecordDecl *RD = getBaseRecordDecl(Ty);
if (!RD)
return true;
const RecordDecl *CanonicalRD = dyn_cast<RecordDecl>(RD->getCanonicalDecl());
if (CanonicalRD == TheRecordDecl) {
SafeToRemoveName = false;
}
return SafeToRemoveName;
}
SimplifyStructUnionDecl::~SimplifyStructUnionDecl(void)
{
delete AnalysisVisitor;
}