forked from vgvassilev/creduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveBaseClass.cpp
303 lines (259 loc) · 9.34 KB
/
RemoveBaseClass.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//===----------------------------------------------------------------------===//
//
// 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 "RemoveBaseClass.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/SourceManager.h"
#include "CommonRenameClassRewriteVisitor.h"
#include "TransformationManager.h"
using namespace clang;
using namespace llvm;
using namespace clang_delta_common_visitor;
static const char *DescriptionMsg =
"This pass removes a base class from its class hierarchy if \n\
* it has less than or equal to 5 declarations, and \n\
* it is not a templated class. \n\
All its declarations will be moved into one of its subclasses, \
and all references to this base class will be replaced with \
the corresponding subclass. \n";
// Note that this pass doesn't do much analysis, so
// it will produce quite a few incompilable code, especially
// when multi-inheritance is involved.
static RegisterTransformation<RemoveBaseClass>
Trans("remove-base-class", DescriptionMsg);
class RemoveBaseClassBaseVisitor : public
RecursiveASTVisitor<RemoveBaseClassBaseVisitor> {
public:
explicit RemoveBaseClassBaseVisitor(
RemoveBaseClass *Instance)
: ConsumerInstance(Instance)
{ }
bool VisitCXXRecordDecl(CXXRecordDecl *CXXRD);
private:
RemoveBaseClass *ConsumerInstance;
};
bool RemoveBaseClassBaseVisitor::VisitCXXRecordDecl(
CXXRecordDecl *CXXRD)
{
ConsumerInstance->handleOneCXXRecordDecl(CXXRD);
return true;
}
class RemoveBaseClassRewriteVisitor : public
CommonRenameClassRewriteVisitor<RemoveBaseClassRewriteVisitor>
{
public:
RemoveBaseClassRewriteVisitor(Rewriter *RT,
RewriteUtils *Helper,
const CXXRecordDecl *CXXRD,
const std::string &Name)
: CommonRenameClassRewriteVisitor<RemoveBaseClassRewriteVisitor>
(RT, Helper, CXXRD, Name)
{ }
};
void RemoveBaseClass::Initialize(ASTContext &context)
{
Transformation::Initialize(context);
CollectionVisitor = new RemoveBaseClassBaseVisitor(this);
}
void RemoveBaseClass::HandleTranslationUnit(ASTContext &Ctx)
{
if (TransformationManager::isCLangOpt()) {
ValidInstanceNum = 0;
}
else {
CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
}
if (QueryInstanceOnly)
return;
if (TransformationCounter > ValidInstanceNum) {
TransError = TransMaxInstanceError;
return;
}
TransAssert(TheBaseClass && "TheBaseClass is NULL!");
TransAssert(TheDerivedClass && "TheDerivedClass is NULL!");
Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
RewriteVisitor =
new RemoveBaseClassRewriteVisitor(&TheRewriter, RewriteHelper,
TheBaseClass->getCanonicalDecl(),
TheDerivedClass->getNameAsString());
TransAssert(RewriteVisitor && "NULL RewriteVisitor!");
RewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
doRewrite();
if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
TransError = TransInternalError;
}
bool RemoveBaseClass::isDirectlyDerivedFrom(const CXXRecordDecl *SubC,
const CXXRecordDecl *Base)
{
for (CXXRecordDecl::base_class_const_iterator I = SubC->bases_begin(),
E = SubC->bases_end(); I != E; ++I) {
if (I->getType()->isDependentType())
continue;
const CXXRecordDecl *BaseDecl =
dyn_cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
if (Base->getCanonicalDecl() == BaseDecl->getCanonicalDecl())
return true;
}
return false;
}
void RemoveBaseClass::handleOneCXXRecordDecl(const CXXRecordDecl *CXXRD)
{
if (isSpecialRecordDecl(CXXRD) || CXXRD->getDescribedClassTemplate() ||
!CXXRD->hasDefinition())
return;
const CXXRecordDecl *CanonicalRD = CXXRD->getCanonicalDecl();
if (VisitedCXXRecordDecls.count(CanonicalRD))
return;
VisitedCXXRecordDecls.insert(CanonicalRD);
if (CanonicalRD->getNumBases()) {
const CXXRecordDecl *Base = NULL;
for (CXXRecordDeclSet::iterator I = AllBaseClasses.begin(),
E = AllBaseClasses.end(); I != E; ++I) {
if (const ClassTemplateSpecializationDecl * CTSD =
dyn_cast<ClassTemplateSpecializationDecl>
(CanonicalRD->getDefinition())) {
if (!CTSD->isExplicitSpecialization())
continue;
}
if (isDirectlyDerivedFrom(CanonicalRD, *I)) {
Base = (*I);
ValidInstanceNum++;
if (ValidInstanceNum == TransformationCounter) {
TransAssert(Base->hasDefinition() &&
"Base class does not have any definition!");
TheBaseClass = Base->getDefinition();
TransAssert(CanonicalRD->hasDefinition() &&
"Derived class does not have any definition!");
TheDerivedClass = CanonicalRD->getDefinition();
}
}
}
return;
}
if (getNumExplicitDecls(CanonicalRD) > MaxNumDecls)
return;
if (!AllBaseClasses.count(CanonicalRD))
AllBaseClasses.insert(CanonicalRD);
}
void RemoveBaseClass::doRewrite(void)
{
copyBaseClassDecls();
removeBaseSpecifier();
RewriteHelper->removeClassDecls(TheBaseClass);
// ISSUE: I didn't handle Base initializer in a Ctor's initlist.
// * keeping it untouched is wrong, because delegating constructors
// are only valie in c++11
// * naively removing the base initializer doesn't work in some cases,
// e.g.,
// class A {
// A(A&) {}
// A &a;
// };
// class C : A {
// C(A &x) : A(x) {}
// };
// during transformation, removing A(x) will leave &a un-initialized.
// I chose to simply delete the base initializer. Seemingly we will
// generate fewer incompilable code by doing so...
removeBaseInitializer();
}
// ISSUE: directly copying decls could bring in name conflicts
void RemoveBaseClass::copyBaseClassDecls(void)
{
if (!getNumExplicitDecls(TheBaseClass))
return;
SourceLocation StartLoc =
RewriteHelper->getLocationAfter(TheBaseClass->getLocation(), '{');
SourceLocation EndLoc = TheBaseClass->getRBraceLoc();
TransAssert(EndLoc.isValid() && "Invalid RBraceLoc!");
EndLoc = EndLoc.getLocWithOffset(-1);
std::string DeclsStr =
TheRewriter.getRewrittenText(SourceRange(StartLoc, EndLoc));
TransAssert(!DeclsStr.empty() && "Empty DeclsStr!");
SourceLocation InsertLoc = TheDerivedClass->getRBraceLoc();
TheRewriter.InsertTextBefore(InsertLoc, DeclsStr);
}
bool RemoveBaseClass::isTheBaseClass(const CXXBaseSpecifier &Specifier)
{
const Type *Ty = TheBaseClass->getTypeForDecl();
return Context->hasSameType(Specifier.getType(),
Ty->getCanonicalTypeInternal());
}
void RemoveBaseClass::removeBaseSpecifier(void)
{
unsigned NumBases = TheDerivedClass->getNumBases();
TransAssert((NumBases >= 1) && "TheDerivedClass doesn't have any base!");
if (NumBases == 1) {
SourceLocation StartLoc = TheDerivedClass->getLocation();
StartLoc = RewriteHelper->getLocationUntil(StartLoc, ':');
SourceLocation EndLoc = RewriteHelper->getLocationUntil(StartLoc, '{');
EndLoc = EndLoc.getLocWithOffset(-1);
TheRewriter.RemoveText(SourceRange(StartLoc, EndLoc));
return;
}
CXXRecordDecl::base_class_const_iterator I = TheDerivedClass->bases_begin();
// remove 'Y,' in code like 'class X : public Y, Z {};'
if (isTheBaseClass(*I)) {
RewriteHelper->removeTextUntil((*I).getSourceRange(), ',');
return;
}
++I;
CXXRecordDecl::base_class_const_iterator E = TheDerivedClass->bases_end();
for (; I != E; ++I) {
if (isTheBaseClass(*I)) {
// remove ',Z' in code like 'class X : public Y, Z {};'
SourceRange Range = (*I).getSourceRange();
SourceLocation EndLoc = RewriteHelper->getEndLocationFromBegin(Range);
RewriteHelper->removeTextFromLeftAt(Range, ',', EndLoc);
return;
}
}
TransAssert(0 && "Unreachable code!");
}
void RemoveBaseClass::rewriteOneCtor(const CXXConstructorDecl *Ctor)
{
unsigned Idx = 0;
const CXXCtorInitializer *Init = NULL;
for (CXXConstructorDecl::init_const_iterator I = Ctor->init_begin(),
E = Ctor->init_end(); I != E; ++I) {
if (!(*I)->isWritten())
continue;
if ((*I)->isBaseInitializer()) {
const Type *Ty = (*I)->getBaseClass();
TransAssert(Ty && "Invalid Base Class Type!");
if (Context->hasSameType(Ty->getCanonicalTypeInternal(),
TheBaseClass->getTypeForDecl()->getCanonicalTypeInternal())) {
Init = (*I);
break;
}
}
Idx++;
}
if (Init) {
RewriteHelper->removeCXXCtorInitializer(Init, Idx,
getNumCtorWrittenInitializers(*Ctor));
}
}
void RemoveBaseClass::removeBaseInitializer(void)
{
for (CXXRecordDecl::ctor_iterator I = TheDerivedClass->ctor_begin(),
E = TheDerivedClass->ctor_end(); I != E; ++I) {
if ((*I)->isThisDeclarationADefinition() && !(*I)->isDefaulted())
rewriteOneCtor(*I);
}
}
RemoveBaseClass::~RemoveBaseClass(void)
{
delete CollectionVisitor;
delete RewriteVisitor;
}