forked from vgvassilev/creduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveUnusedOuterClass.cpp
153 lines (126 loc) · 4.23 KB
/
RemoveUnusedOuterClass.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
//===----------------------------------------------------------------------===//
//
// 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 "RemoveUnusedOuterClass.h"
#include "clang/Basic/SourceManager.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTContext.h"
#include "clang/Lex/Lexer.h"
#include "TransformationManager.h"
using namespace clang;
using namespace llvm;
static const char *DescriptionMsg =
"This pass an unused outer class if \n\
* the outer class doesn't have any base class, and \n\
* the outer class does not have any described template, and \n\
* the outer class is not been referenced \n\
";
static RegisterTransformation<RemoveUnusedOuterClass>
Trans("remove-unused-outer-class", DescriptionMsg);
class RemoveUnusedOuterClassVisitor : public
RecursiveASTVisitor<RemoveUnusedOuterClassVisitor> {
public:
explicit RemoveUnusedOuterClassVisitor(
RemoveUnusedOuterClass *Instance)
: ConsumerInstance(Instance)
{ }
bool VisitCXXRecordDecl(CXXRecordDecl *CXXRD);
bool VisitRecordTypeLoc(RecordTypeLoc TLoc);
private:
RemoveUnusedOuterClass *ConsumerInstance;
};
bool RemoveUnusedOuterClassVisitor::VisitRecordTypeLoc(RecordTypeLoc TLoc)
{
const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TLoc.getDecl());
ConsumerInstance->UsedCXXRDSet.insert(RD->getCanonicalDecl());
return true;
}
bool RemoveUnusedOuterClassVisitor::VisitCXXRecordDecl(
CXXRecordDecl *CXXRD)
{
if (ConsumerInstance->isSpecialRecordDecl(CXXRD) ||
!CXXRD->hasDefinition() ||
dyn_cast<ClassTemplateSpecializationDecl>(CXXRD) ||
CXXRD->hasUserDeclaredConstructor() ||
CXXRD->hasUserDeclaredDestructor() ||
CXXRD->getDescribedClassTemplate() ||
CXXRD->getNumBases())
return true;
ConsumerInstance->CXXRDDefSet.insert(CXXRD->getDefinition());
return true;
}
void RemoveUnusedOuterClass::Initialize(ASTContext &context)
{
Transformation::Initialize(context);
CollectionVisitor = new RemoveUnusedOuterClassVisitor(this);
}
void RemoveUnusedOuterClass::HandleTranslationUnit(ASTContext &Ctx)
{
if (TransformationManager::isCLangOpt()) {
ValidInstanceNum = 0;
}
else {
CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
analyzeCXXRDSet();
}
if (QueryInstanceOnly)
return;
if (TransformationCounter > ValidInstanceNum) {
TransError = TransMaxInstanceError;
return;
}
Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
removeOuterClass();
if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
TransError = TransInternalError;
}
void RemoveUnusedOuterClass::analyzeCXXRDSet()
{
for (CXXRecordDeclSet::iterator I = CXXRDDefSet.begin(),
E = CXXRDDefSet.end(); I != E; ++I) {
const CXXRecordDecl *Def = (*I);
if (UsedCXXRDSet.count(Def->getCanonicalDecl()))
continue;
ValidInstanceNum++;
if (ValidInstanceNum == TransformationCounter)
TheCXXRDDef = Def;
}
}
void RemoveUnusedOuterClass::removeOuterClass()
{
TransAssert(TheCXXRDDef && "NULL Base CXXRD!");
SourceLocation LocStart = TheCXXRDDef->getLocStart();
SourceLocation LocEnd =
RewriteHelper->getEndLocationUntil(LocStart, '{');
TransAssert(LocEnd.isValid() && "Invalid Location!");
TheRewriter.RemoveText(SourceRange(LocStart, LocEnd));
const DeclContext *Ctx = dyn_cast<DeclContext>(TheCXXRDDef);
for (DeclContext::decl_iterator I = Ctx->decls_begin(),
E = Ctx->decls_end(); I != E; ++I) {
if ((*I)->isImplicit())
continue;
const AccessSpecDecl *AS = dyn_cast<AccessSpecDecl>(*I);
if (!AS)
continue;
TheRewriter.RemoveText(AS->getSourceRange());
}
LocStart = TheCXXRDDef->getRBraceLoc();
LocEnd = RewriteHelper->getLocationUntil(LocStart, ';');
if (LocStart.isInvalid() || LocEnd.isInvalid())
return;
TheRewriter.RemoveText(SourceRange(LocStart, LocEnd));
}
RemoveUnusedOuterClass::~RemoveUnusedOuterClass(void)
{
delete CollectionVisitor;
}