forked from vgvassilev/creduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimplifyRecursiveTemplateInstantiation.cpp
204 lines (170 loc) · 6.16 KB
/
SimplifyRecursiveTemplateInstantiation.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
//===----------------------------------------------------------------------===//
//
// 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 "SimplifyRecursiveTemplateInstantiation.h"
#include "clang/Lex/Lexer.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 handles a special case where we have recursive template \
instantiations. For example, given the code blow: \n\
C<B<C<B<C<B<D>, G<int> > > \n\
it will replace an outer instantiation of B with an inner one, e.g. B<D>.\n\
The the number of transformation instances counts from inside to \n\
outside. Currently we only handle class template instantiations. \n\
";
static RegisterTransformation<SimplifyRecursiveTemplateInstantiation>
Trans("simplify-recursive-template-instantiation", DescriptionMsg);
namespace {
class LocalTemplateArgVisitor : public
RecursiveASTVisitor<LocalTemplateArgVisitor> {
public:
LocalTemplateArgVisitor(const TemplateSpecializationTypeLoc &TLoc,
SimplifyRecursiveTemplateInstantiation *Instance)
: ParentTLoc(TLoc),
ConsumerInstance(Instance)
{ }
bool VisitTemplateSpecializationTypeLoc(
TemplateSpecializationTypeLoc TLoc);
private:
const TemplateSpecializationTypeLoc &ParentTLoc;
SimplifyRecursiveTemplateInstantiation *ConsumerInstance;
};
bool LocalTemplateArgVisitor::VisitTemplateSpecializationTypeLoc(
TemplateSpecializationTypeLoc TLoc)
{
ConsumerInstance->handleInnerTemplateSpecializationTypeLoc(ParentTLoc, TLoc);
return true;
}
} // end anon namespace
class SimplifyRecursiveTemplateInstantiationASTVisitor : public
RecursiveASTVisitor<SimplifyRecursiveTemplateInstantiationASTVisitor> {
public:
explicit SimplifyRecursiveTemplateInstantiationASTVisitor(
SimplifyRecursiveTemplateInstantiation *Instance)
: ConsumerInstance(Instance)
{ }
bool VisitTemplateSpecializationTypeLoc(
TemplateSpecializationTypeLoc TLoc);
private:
SimplifyRecursiveTemplateInstantiation *ConsumerInstance;
};
bool SimplifyRecursiveTemplateInstantiationASTVisitor::
VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TLoc)
{
ConsumerInstance->handleTemplateSpecializationTypeLoc(TLoc);
return true;
}
void SimplifyRecursiveTemplateInstantiation::Initialize(ASTContext &context)
{
Transformation::Initialize(context);
CollectionVisitor =
new SimplifyRecursiveTemplateInstantiationASTVisitor(this);
}
void
SimplifyRecursiveTemplateInstantiation::HandleTranslationUnit(ASTContext &Ctx)
{
if (TransformationManager::isCLangOpt()) {
ValidInstanceNum = 0;
}
else {
CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
analyzeLocPairs();
}
if (QueryInstanceOnly)
return;
if (TransformationCounter > ValidInstanceNum) {
TransError = TransMaxInstanceError;
return;
}
Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
rewriteTemplateArgument();
if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
TransError = TransInternalError;
}
void SimplifyRecursiveTemplateInstantiation::analyzeLocPairs()
{
for (SpecTypeLocPairQueue::reverse_iterator I = LocPairQueue.rbegin(),
E = LocPairQueue.rend(); I != E; ++I) {
ValidInstanceNum++;
if (ValidInstanceNum == TransformationCounter)
TheLocPair = (*I);
}
}
void
SimplifyRecursiveTemplateInstantiation::handleTemplateSpecializationTypeLoc(
const TemplateSpecializationTypeLoc &TLoc)
{
for (unsigned I = 0; I < TLoc.getNumArgs(); ++I) {
TemplateArgumentLoc ArgLoc = TLoc.getArgLoc(I);
if (ArgLoc.getLocation().isInvalid())
continue;
LocalTemplateArgVisitor ArgVisitor(TLoc, this);
ArgVisitor.TraverseTemplateArgumentLoc(ArgLoc);
}
}
const TemplateDecl *
SimplifyRecursiveTemplateInstantiation::getTemplateDeclFromSpecLoc(
const TemplateSpecializationTypeLoc &TLoc)
{
const Type *Ty = TLoc.getTypePtr();
const TemplateSpecializationType *TST =
Ty->getAs<TemplateSpecializationType>();
TemplateName TplName = TST->getTemplateName();
return TplName.getAsTemplateDecl();
}
void SimplifyRecursiveTemplateInstantiation::
handleInnerTemplateSpecializationTypeLoc(
const TemplateSpecializationTypeLoc &ParentTLoc,
const TemplateSpecializationTypeLoc &TLoc)
{
const TemplateDecl *ParentD = getTemplateDeclFromSpecLoc(ParentTLoc);
TransAssert(ParentD && "NULL ParentD!");
const TemplateDecl *D = getTemplateDeclFromSpecLoc(TLoc);
TransAssert(D && "NULL Decl!");
if (ParentD->getCanonicalDecl() != D->getCanonicalDecl())
return;
SpecTypeLocPair *P = new SpecTypeLocPair();
TransAssert(P && "Failed to alloc SpecTypeLocPair!");
P->push_back(ParentTLoc);
P->push_back(TLoc);
LocPairQueue.push_back(P);
}
void SimplifyRecursiveTemplateInstantiation::rewriteTemplateArgument()
{
TransAssert(TheLocPair && "NULL TheLocPair!");
TransAssert((TheLocPair->size() == 2) && "Invalid size of TheLocPair!");
TemplateSpecializationTypeLoc TLoc = TheLocPair->pop_back_val();
TemplateSpecializationTypeLoc ParentTLoc = TheLocPair->pop_back_val();
SourceLocation LAngleLoc = TLoc.getLAngleLoc();
SourceLocation RAngleLoc = TLoc.getRAngleLoc();
SourceLocation ParentLAngleLoc = ParentTLoc.getLAngleLoc();
SourceLocation ParentRAngleLoc = ParentTLoc.getRAngleLoc();
std::string InnerStr = "";
RewriteHelper->getStringBetweenLocs(InnerStr, LAngleLoc, RAngleLoc);
TheRewriter.ReplaceText(SourceRange(ParentLAngleLoc, ParentRAngleLoc),
InnerStr + ">");
}
SimplifyRecursiveTemplateInstantiation::
~SimplifyRecursiveTemplateInstantiation()
{
for (SpecTypeLocPairQueue::iterator I = LocPairQueue.begin(),
E = LocPairQueue.end(); I != E; ++I) {
delete (*I);
}
delete CollectionVisitor;
}