forked from vgvassilev/creduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveAddrTaken.cpp
195 lines (152 loc) · 4.69 KB
/
RemoveAddrTaken.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
//===----------------------------------------------------------------------===//
//
// 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 "RemoveAddrTaken.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 =
"Remove an addr-taken operator if \n\
* the subexpr is type of pointer, or \n\
* the subexpr is type of integer and the addr-taken operator is \
an operand of a comparison operator, or \n\
* the entire addr-taken expr is an argument of a function, \
and the argument doesn't have a correponding parameter in function's \
declaration.\n";
static RegisterTransformation<RemoveAddrTaken>
Trans("remove-addr-taken", DescriptionMsg);
class RemoveAddrTakenCollectionVisitor : public
RecursiveASTVisitor<RemoveAddrTakenCollectionVisitor> {
public:
explicit RemoveAddrTakenCollectionVisitor(RemoveAddrTaken *Instance)
: ConsumerInstance(Instance)
{ }
bool VisitUnaryOperator(UnaryOperator *UO);
bool VisitBinaryOperator(BinaryOperator *BO);
bool VisitCallExpr(CallExpr *CE);
private:
void handleOneAddrTakenOp(const UnaryOperator *UO);
void handleOneOperand(const Expr *E);
RemoveAddrTaken *ConsumerInstance;
};
void RemoveAddrTakenCollectionVisitor::handleOneAddrTakenOp(
const UnaryOperator *UO)
{
if (ConsumerInstance->VisitedAddrTakenOps.count(UO))
return;
ConsumerInstance->VisitedAddrTakenOps.insert(UO);
ConsumerInstance->ValidInstanceNum++;
if (ConsumerInstance->TransformationCounter ==
ConsumerInstance->ValidInstanceNum)
ConsumerInstance->TheUO = UO;
}
bool RemoveAddrTakenCollectionVisitor::VisitUnaryOperator(UnaryOperator *UO)
{
if (UO->getOpcode() != UO_AddrOf)
return true;
const Expr *E = UO->getSubExpr();
const Type *Ty = E->getType().getTypePtr();
if (!Ty->isPointerType())
return true;
handleOneAddrTakenOp(UO);
return true;
}
void RemoveAddrTakenCollectionVisitor::handleOneOperand(const Expr *E)
{
const UnaryOperator *UO = dyn_cast<UnaryOperator>(E);
if (!UO)
return;
if (UO->getOpcode() != UO_AddrOf)
return;
const Expr *SubE = UO->getSubExpr();
const Type *Ty = SubE->getType().getTypePtr();
if (!Ty->isIntegerType())
return;
handleOneAddrTakenOp(UO);
}
bool RemoveAddrTakenCollectionVisitor::VisitBinaryOperator(BinaryOperator *BO)
{
if (!BO->isComparisonOp())
return true;
handleOneOperand(BO->getLHS());
handleOneOperand(BO->getRHS());
return true;
}
// handle special cases like
// void f1();
// void f2(void) {
// f1(xxx);
// }
bool RemoveAddrTakenCollectionVisitor::VisitCallExpr(CallExpr *CE)
{
const FunctionDecl *FD = CE->getDirectCallee();
if (!FD)
return true;
unsigned NumParams = FD->getNumParams();
if (NumParams != 0)
return true;
for (CallExpr::arg_iterator I = CE->arg_begin(),
E = CE->arg_end(); I != E; ++I) {
const Expr *Arg = (*I);
const UnaryOperator *UO = dyn_cast<UnaryOperator>(Arg);
if (!UO || (UO->getOpcode() != UO_AddrOf))
continue;
handleOneAddrTakenOp(UO);
}
return true;
}
void RemoveAddrTaken::Initialize(ASTContext &context)
{
Transformation::Initialize(context);
CollectionVisitor = new RemoveAddrTakenCollectionVisitor(this);
}
bool RemoveAddrTaken::HandleTopLevelDecl(DeclGroupRef D)
{
TransAssert(CollectionVisitor && "NULL CollectionVisitor!");
if (TransformationManager::isCXXLangOpt()) {
ValidInstanceNum = 0;
return true;
}
for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
FunctionDecl *FD = dyn_cast<FunctionDecl>(*I);
if (!FD || !FD->isThisDeclarationADefinition())
continue;
CollectionVisitor->TraverseDecl(*I);
}
return true;
}
void RemoveAddrTaken::HandleTranslationUnit(ASTContext &Ctx)
{
if (QueryInstanceOnly)
return;
if (TransformationCounter > ValidInstanceNum) {
TransError = TransMaxInstanceError;
return;
}
TransAssert(TheUO && "NULL UnaryOperator!");
rewriteAddrTakenOp(TheUO);
if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
TransError = TransInternalError;
}
void RemoveAddrTaken::rewriteAddrTakenOp(const UnaryOperator *UO)
{
SourceLocation Loc = UO->getOperatorLoc();
TheRewriter.RemoveText(Loc, 1);
}
RemoveAddrTaken::~RemoveAddrTaken(void)
{
delete CollectionVisitor;
}