forked from vgvassilev/creduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimplifyIf.cpp
211 lines (168 loc) · 4.77 KB
/
SimplifyIf.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
//===----------------------------------------------------------------------===//
//
// 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 "SimplifyIf.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/SourceManager.h"
#include "TransformationManager.h"
#include "CommonStatementVisitor.h"
using namespace clang;
static const char *DescriptionMsg =
"Simplify an if-else statement. It transforms the following code: \n\
if (guard1) \n\
{... } \n\
else if (guard2) \n\
else \n\
{...} \n\
to \n\
(guard1) \n\
{... } \n\
if (guard2) \n\
else \n\
{...} \n\
if there is no else-if left, the last else keyword will be removed. \n";
class SimplifyIfCollectionVisitor : public
RecursiveASTVisitor<SimplifyIfCollectionVisitor> {
public:
explicit SimplifyIfCollectionVisitor(SimplifyIf *Instance)
: ConsumerInstance(Instance)
{ }
bool VisitFunctionDecl(FunctionDecl *FD);
private:
SimplifyIf *ConsumerInstance;
};
static RegisterTransformation<SimplifyIf>
Trans("simplify-if", DescriptionMsg);
class SimplifyIfStatementVisitor : public
CommonStatementVisitor<SimplifyIfStatementVisitor> {
public:
explicit SimplifyIfStatementVisitor(SimplifyIf *Instance)
: ConsumerInstance(Instance)
{ }
bool VisitIfStmt(IfStmt *IS);
bool VisitForStmt(ForStmt *FS);
bool VisitWhileStmt(WhileStmt *WS);
bool VisitDoStmt(DoStmt *DS);
private:
SimplifyIf *ConsumerInstance;
};
bool SimplifyIfCollectionVisitor::VisitFunctionDecl(FunctionDecl *FD)
{
if (!FD->isThisDeclarationADefinition())
return true;
ConsumerInstance->StmtVisitor->TraverseDecl(FD);
return true;
}
// It is used to handle the case where if-then or else branch
// is not treated as a CompoundStmt. So it cannot be traversed
// from VisitCompoundStmt, e.g.,
// if (x)
// foo(bar())
bool SimplifyIfStatementVisitor::VisitIfStmt(IfStmt *IS)
{
ConsumerInstance->ValidInstanceNum++;
if (ConsumerInstance->ValidInstanceNum ==
ConsumerInstance->TransformationCounter) {
ConsumerInstance->TheIfStmt = IS;
ConsumerInstance->NeedParen = NeedParen;
}
Stmt *ThenB = IS->getThen();
visitNonCompoundStmt(ThenB);
Stmt *ElseB = IS->getElse();
visitNonCompoundStmt(ElseB);
return false;
}
// It causes unsound transformation because
// the semantics of loop execution has been changed.
// For example,
// int foo(int x)
// {
// int i;
// for(i = 0; i < bar(bar(x)); i++)
// ...
// }
// will be transformed to:
// int foo(int x)
// {
// int i;
// int tmp_var = bar(x);
// for(i = 0; i < bar(tmp_var); i++)
// ...
// }
bool SimplifyIfStatementVisitor::VisitForStmt(ForStmt *FS)
{
Stmt *Body = FS->getBody();
visitNonCompoundStmt(Body);
return false;
}
bool SimplifyIfStatementVisitor::VisitWhileStmt(WhileStmt *WS)
{
Stmt *Body = WS->getBody();
visitNonCompoundStmt(Body);
return false;
}
bool SimplifyIfStatementVisitor::VisitDoStmt(DoStmt *DS)
{
Stmt *Body = DS->getBody();
visitNonCompoundStmt(Body);
return false;
}
void SimplifyIf::Initialize(ASTContext &context)
{
Transformation::Initialize(context);
CollectionVisitor = new SimplifyIfCollectionVisitor(this);
StmtVisitor = new SimplifyIfStatementVisitor(this);
}
bool SimplifyIf::HandleTopLevelDecl(DeclGroupRef D)
{
for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
CollectionVisitor->TraverseDecl(*I);
}
return true;
}
void SimplifyIf::HandleTranslationUnit(ASTContext &Ctx)
{
if (QueryInstanceOnly)
return;
if (TransformationCounter > ValidInstanceNum) {
TransError = TransMaxInstanceError;
return;
}
Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
TransAssert(TheIfStmt && "NULL TheIfStmt");
simplifyIfStmt();
if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
TransError = TransInternalError;
}
void SimplifyIf::simplifyIfStmt(void)
{
const Expr *Cond = TheIfStmt->getCond();
TransAssert(Cond && "Bad Cond Expr!");
std::string CondStr;
RewriteHelper->getExprString(Cond, CondStr);
CondStr += ";";
RewriteHelper->addStringBeforeStmt(TheIfStmt, CondStr, NeedParen);
RewriteHelper->removeIfAndCond(TheIfStmt);
const Stmt *ElseS = TheIfStmt->getElse();
if (ElseS) {
SourceLocation ElseLoc = TheIfStmt->getElseLoc();
std::string ElseStr = "else";
TheRewriter.RemoveText(ElseLoc, ElseStr.size());
}
}
SimplifyIf::~SimplifyIf(void)
{
delete CollectionVisitor;
delete StmtVisitor;
}