forked from vgvassilev/creduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenameClass.cpp
265 lines (214 loc) · 6.63 KB
/
RenameClass.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
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2012, 2013 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 "RenameClass.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 =
"To increase readability, simplify class names to [A - Z] \
(except E, T and Z). Class names from the same hierarchy tree will \
have alphabetical order. For example, we could have a transformed \
code shown as below: \n\
class A {}; \n\
class B : public A {}; \n\
class C : public B {}; \n";
static RegisterTransformation<RenameClass>
Trans("rename-class", DescriptionMsg);
class RenameClassASTVisitor : public
RecursiveASTVisitor<RenameClassASTVisitor> {
public:
explicit RenameClassASTVisitor(RenameClass *Instance)
: ConsumerInstance(Instance)
{ }
bool VisitCXXRecordDecl(CXXRecordDecl *CXXRD);
private:
RenameClass *ConsumerInstance;
};
class RenameClassRewriteVisitor : public
CommonRenameClassRewriteVisitor<RenameClassRewriteVisitor>
{
public:
RenameClassRewriteVisitor(Rewriter *RT,
RewriteUtils *Helper,
const CXXRecordDecl *CXXRD,
const std::string &Name)
: CommonRenameClassRewriteVisitor<RenameClassRewriteVisitor>
(RT, Helper, CXXRD, Name)
{ }
};
bool RenameClassASTVisitor::VisitCXXRecordDecl(CXXRecordDecl *CXXRD)
{
ConsumerInstance->analyzeOneRecordDecl(CXXRD);
return true;
}
void RenameClass::Initialize(ASTContext &context)
{
Transformation::Initialize(context);
CollectionVisitor = new RenameClassASTVisitor(this);
}
void RenameClass::HandleTranslationUnit(ASTContext &Ctx)
{
if (TransformationManager::isCLangOpt()) {
ValidInstanceNum = 0;
}
else {
CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
doAnalysis();
}
if (QueryInstanceOnly)
return;
if ((ValidInstanceNum + UsedNames.size() > 23)) {
TransError = TransMaxClassesError;
return;
}
else if (TransformationCounter > ValidInstanceNum) {
TransError = TransMaxInstanceError;
return;
}
Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
RewriteVisitor =
new RenameClassRewriteVisitor(&TheRewriter, RewriteHelper,
TheCXXRecordDecl, NewNameStr);
TransAssert(RewriteVisitor && "NULL RewriteVisitor!");
RewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
TransError = TransInternalError;
}
bool RenameClass::isReservedName(char C)
{
return ((C == 'E') || (C == 'T') || (C == 'U'));
}
void RenameClass::incValidInstance(const CXXRecordDecl *CXXRD)
{
ValidInstanceNum++;
if (ValidInstanceNum != TransformationCounter)
return;
while (isReservedName(CurrentName) || UsedNames.count(CurrentName)) {
if (CurrentName > 'Z')
return;
CurrentName++;
}
TheCXXRecordDecl = CXXRD;
NewNameStr.assign(1, CurrentName);
}
void RenameClass::doAnalysis(void)
{
for (unsigned Level = 0; Level <= MaxInheritanceLevel; ++Level) {
CXXRecordDeclSet *RDSet = LevelToRecords[Level];
if (!RDSet)
continue;
for (CXXRecordDeclSet::const_iterator I = RDSet->begin(),
E = RDSet->end(); I != E; ++I) {
const CXXRecordDecl *CXXRD = (*I);
if (UsedNameDecls.count(CXXRD->getCanonicalDecl()))
continue;
incValidInstance(CXXRD);
}
}
}
bool RenameClass::isValidName(const std::string &Name)
{
if (Name.size() != 1)
return false;
char C = Name[0];
return (((C >= 'A') || (C <= 'Z')) && !isReservedName(C));
}
void RenameClass::addOneRecordDecl(const CXXRecordDecl *CanonicalRD,
unsigned Level)
{
RecordToLevel[CanonicalRD] = Level;
if (Level > MaxInheritanceLevel)
MaxInheritanceLevel = Level;
CXXRecordDeclSet *RDSet = LevelToRecords[Level];
if (!RDSet) {
RDSet = new CXXRecordDeclSet();
TransAssert(RDSet && "Cannot new a CXXRecordDeclSet!");
LevelToRecords[Level] = RDSet;
}
RDSet->insert(CanonicalRD);
std::string RDName = CanonicalRD->getNameAsString();
if (isValidName(RDName)) {
char C = RDName[0];
UsedNames.insert(C);
UsedNameDecls.insert(CanonicalRD);
}
}
void RenameClass::analyzeOneRecordDecl(const CXXRecordDecl *CXXRD)
{
if (isSpecialRecordDecl(CXXRD))
return;
// Avoid duplication
if (dyn_cast<ClassTemplateSpecializationDecl>(CXXRD))
return;
if (CXXRD->getNameAsString().empty())
return;
const CXXRecordDecl *CanonicalRD = CXXRD->getCanonicalDecl();
if (RecordToLevel.find(CanonicalRD) != RecordToLevel.end())
return;
if (!CXXRD->hasDefinition()) {
addOneRecordDecl(CanonicalRD, 0);
return;
}
// getNumBases dies on the case where CXXRD has no definition.
unsigned NumBases = CanonicalRD->getNumBases();
// in some cases, we will encounter a derived classs before
// its base class,
// namespace NS1 {
// class Derived;
// class Base {};
// class Derived: public Base { };
// }
if (NumBases == 0) {
addOneRecordDecl(CanonicalRD, 0);
return;
}
unsigned Level = 0;
for (CXXRecordDecl::base_class_const_iterator I =
CanonicalRD->bases_begin(), E = CanonicalRD->bases_end(); I != E; ++I) {
const CXXBaseSpecifier *BS = I;
const Type *Ty = BS->getType().getTypePtr();
const CXXRecordDecl *Base = getBaseDeclFromType(Ty);
// If cannot find CXXRD's base class, then we assume its base class
// has the level of 0
if (!Base)
continue;
const CXXRecordDecl *CanonicalBase = Base->getCanonicalDecl();
if (CanonicalBase == CanonicalRD)
continue;
unsigned BaseLevel;
RecordToInheritanceLevelMap::iterator LI =
RecordToLevel.find(CanonicalBase);
if (LI == RecordToLevel.end()) {
continue;
}
BaseLevel = (*LI).second;
if (BaseLevel > Level)
Level = BaseLevel;
}
Level++;
addOneRecordDecl(CanonicalRD, Level);
}
RenameClass::~RenameClass(void)
{
for (InheritanceLevelToRecordsMap::iterator I = LevelToRecords.begin(),
E = LevelToRecords.end(); I != E; ++I) {
delete (*I).second;
}
delete CollectionVisitor;
delete RewriteVisitor;
}