forked from vgvassilev/creduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonStatementVisitor.h
195 lines (154 loc) · 4.4 KB
/
CommonStatementVisitor.h
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
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//
#ifndef COMMON_STATEMENT_VISITOR_H
#define COMMON_STATEMENT_VISITOR_H
#include "clang/AST/RecursiveASTVisitor.h"
template<typename T>
class CommonStatementVisitor : public clang::RecursiveASTVisitor<T> {
public:
CommonStatementVisitor()
: CurrentFuncDecl(NULL),
CurrentStmt(NULL),
NeedParen(false)
{ }
T &getDerived() { return *static_cast<T*>(this); };
void setCurrentFunctionDecl(clang::FunctionDecl *FD) {
CurrentFuncDecl = FD;
}
bool VisitCompoundStmt(clang::CompoundStmt *S);
bool VisitIfStmt(clang::IfStmt *IS);
bool VisitForStmt(clang::ForStmt *FS);
bool VisitWhileStmt(clang::WhileStmt *WS);
bool VisitDoStmt(clang::DoStmt *DS);
bool VisitCaseStmt(clang::CaseStmt *CS);
bool VisitDefaultStmt(clang::DefaultStmt *DS);
bool VisitCXXTryStmt(clang::CXXTryStmt *DS);
void visitNonCompoundStmt(clang::Stmt *S);
protected:
clang::FunctionDecl *CurrentFuncDecl;
clang::Stmt *CurrentStmt;
bool NeedParen;
};
template<typename T>
bool CommonStatementVisitor<T>::VisitCompoundStmt(clang::CompoundStmt *CS)
{
for (clang::CompoundStmt::body_iterator I = CS->body_begin(),
E = CS->body_end(); I != E; ++I) {
CurrentStmt = (*I);
getDerived().TraverseStmt(*I);
}
return false;
}
template<typename T>
void CommonStatementVisitor<T>::visitNonCompoundStmt(clang::Stmt *S)
{
if (!S)
return;
clang::CompoundStmt *CS = llvm::dyn_cast<clang::CompoundStmt>(S);
if (CS) {
VisitCompoundStmt(CS);
return;
}
CurrentStmt = (S);
NeedParen = true;
getDerived().TraverseStmt(S);
NeedParen = false;
}
// 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())
template<typename T>
bool CommonStatementVisitor<T>::VisitIfStmt(clang::IfStmt *IS)
{
clang::Expr *E = IS->getCond();
getDerived().TraverseStmt(E);
clang::Stmt *ThenB = IS->getThen();
visitNonCompoundStmt(ThenB);
clang::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++)
// ...
// }
template<typename T>
bool CommonStatementVisitor<T>::VisitForStmt(clang::ForStmt *FS)
{
clang::Stmt *Init = FS->getInit();
getDerived().TraverseStmt(Init);
clang::Expr *Cond = FS->getCond();
getDerived().TraverseStmt(Cond);
clang::Expr *Inc = FS->getInc();
getDerived().TraverseStmt(Inc);
clang::Stmt *Body = FS->getBody();
visitNonCompoundStmt(Body);
return false;
}
template<typename T>
bool CommonStatementVisitor<T>::VisitWhileStmt(clang::WhileStmt *WS)
{
clang::Expr *E = WS->getCond();
getDerived().TraverseStmt(E);
clang::Stmt *Body = WS->getBody();
visitNonCompoundStmt(Body);
return false;
}
template<typename T>
bool CommonStatementVisitor<T>::VisitDoStmt(clang::DoStmt *DS)
{
clang::Expr *E = DS->getCond();
getDerived().TraverseStmt(E);
clang::Stmt *Body = DS->getBody();
visitNonCompoundStmt(Body);
return false;
}
template<typename T>
bool CommonStatementVisitor<T>::VisitCaseStmt(clang::CaseStmt *CS)
{
clang::Stmt *Body = CS->getSubStmt();
visitNonCompoundStmt(Body);
return false;
}
template<typename T>
bool CommonStatementVisitor<T>::VisitDefaultStmt(clang::DefaultStmt *DS)
{
clang::Stmt *Body = DS->getSubStmt();
visitNonCompoundStmt(Body);
return false;
}
template<typename T>
bool CommonStatementVisitor<T>::VisitCXXTryStmt(clang::CXXTryStmt *CS)
{
clang::CompoundStmt *TryBlock = CS->getTryBlock();
visitNonCompoundStmt(TryBlock);
for (unsigned I = 0; I < CS->getNumHandlers(); ++I) {
clang::CXXCatchStmt *CatchStmt = CS->getHandler(I);
clang::Stmt *CatchBlock = CatchStmt->getHandlerBlock();
visitNonCompoundStmt(CatchBlock);
}
return false;
}
#endif