forked from vgvassilev/creduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RewriteUtils.h
317 lines (217 loc) · 10.4 KB
/
RewriteUtils.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//===----------------------------------------------------------------------===//
//
// 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 REWRITE_UTILS_H
#define REWRITE_UTILS_H
#include <string>
#include "clang/Basic/SourceLocation.h"
#include "clang/AST/NestedNameSpecifier.h"
#ifndef ENABLE_TRANS_ASSERT
#define TransAssert(x) {if (!(x)) exit(0);}
#else
#define TransAssert(x) assert(x)
#endif
namespace clang {
class ParmVarDecl;
class VarDecl;
class Decl;
class DeclStmt;
class Rewriter;
class SourceManager;
class CallExpr;
class Expr;
class FunctionDecl;
class TemplateDecl;
class Stmt;
class DeclGroupRef;
class DeclRefExpr;
class NamedDecl;
class IfStmt;
class Type;
class FieldDecl;
class CXXConstructExpr;
class RecordDecl;
class CXXMemberCallExpr;
class RecordTypeLoc;
class CXXDestructorDecl;
class CXXCtorInitializer;
class CXXRecordDecl;
class ClassTemplateDecl;
class CXXMethodDecl;
class NestedNameSpecifierLoc;
}
class RewriteUtils {
public:
static RewriteUtils *GetInstance(clang::Rewriter *RW);
static void Finalize(void);
clang::SourceLocation getEndLocationFromBegin(clang::SourceRange Range);
bool removeParamFromFuncDecl(const clang::ParmVarDecl *PV,
unsigned int NumParams,
int ParamPos);
bool removeArgFromCallExpr(const clang::CallExpr *CallE,
int ParamPos);
bool removeArgFromCXXConstructExpr(const clang::CXXConstructExpr *CE,
int ParamPos);
bool removeVarFromDeclStmt(clang::DeclStmt *DS,
const clang::VarDecl *VD,
clang::Decl *PrevDecl,
bool IsFirstDecl);
bool getExprString(const clang::Expr *E,
std::string &ES);
bool getStmtString(const clang::Stmt *S,
std::string &Str);
bool replaceExpr(const clang::Expr *E,
const std::string &ES);
bool replaceExprNotInclude(const clang::Expr *E,
const std::string &ES);
bool addLocalVarToFunc(const std::string &VarStr,
clang::FunctionDecl *FD);
std::string getStmtIndentString(clang::Stmt *S,
clang::SourceManager *SrcManager);
bool addNewAssignStmtBefore(clang::Stmt *BeforeStmt,
const std::string &VarName,
clang::Expr *RHS,
bool NeedParen);
bool addStringBeforeStmt(clang::Stmt *BeforeStmt,
const std::string &Str,
bool NeedParen);
bool addStringAfterStmt(clang::Stmt *AfterStmt,
const std::string &Str);
bool addStringAfterVarDecl(const clang::VarDecl *VD,
const std::string &Str);
bool addStringAfterFuncDecl(const clang::FunctionDecl *FD,
const std::string &Str);
bool replaceVarDeclName(clang::VarDecl *VD,
const std::string &NameStr);
bool replaceFunctionDeclName(const clang::FunctionDecl *FD,
const std::string &NameStr);
bool replaceRecordDeclName(const clang::RecordDecl *RD,
const std::string &NameStr);
bool replaceVarTypeName(const clang::VarDecl *VD,
const std::string &NameStr);
const char *getTmpVarNamePrefix(void);
void getStringBetweenLocs(std::string &Str,
clang::SourceLocation LocStart,
clang::SourceLocation LocEnd);
void getStringBetweenLocsAfterStart(std::string &Str,
clang::SourceLocation LocStart,
clang::SourceLocation LocEnd);
bool getDeclGroupStrAndRemove(clang::DeclGroupRef DGR,
std::string &Str);
bool getEntireDeclGroupStrAndRemove(clang::DeclGroupRef DGR,
std::string &Str);
clang::SourceLocation getDeclGroupRefEndLoc(clang::DeclGroupRef DGR);
bool getDeclStmtStrAndRemove(clang::DeclStmt *DS,
std::string &Str);
clang::SourceLocation getDeclStmtEndLoc(clang::DeclStmt *DS);
bool removeAStarBefore(const clang::Decl *D);
bool removeAStarAfter(const clang::Expr *E);
bool removeAnAddrOfAfter(const clang::Expr *E);
bool removeASymbolAfter(const clang::Expr *E, char Symbol);
bool insertAnAddrOfBefore(const clang::Expr *E);
bool insertAStarBefore(const clang::Expr *E);
bool removeVarInitExpr(const clang::VarDecl *VD);
bool removeVarDecl(const clang::VarDecl *VD,
clang::DeclGroupRef DGR);
bool removeVarDecl(const clang::VarDecl *VD);
void getTmpTransName(unsigned Postfix, std::string &Name);
bool insertStringBeforeFunc(const clang::FunctionDecl *FD,
const std::string &Str);
bool insertStringBeforeTemplateDecl(const clang::TemplateDecl *D,
const std::string &Str);
bool replaceUnionWithStruct(const clang::NamedDecl *ND);
bool removeIfAndCond(const clang::IfStmt *IS);
clang::SourceLocation getLocationUntil(clang::SourceLocation Loc,
char Symbol);
clang::SourceLocation getLocationAfter(clang::SourceLocation Loc,
char Symbol);
bool removeArraySubscriptExpr(const clang::Expr *E);
bool getFunctionDefStrAndRemove(const clang::FunctionDecl *FD,
std::string &Str);
bool getFunctionDeclStrAndRemove(const clang::FunctionDecl *FD,
std::string &Str);
bool replaceFunctionDefWithStr(const clang::FunctionDecl *FD,
const std::string &Str);
clang::SourceLocation getEndLocationUntil(clang::SourceRange Range,
char Symbol);
bool removeFieldDecl(const clang::FieldDecl *FD);
bool removeDecl(const clang::Decl *D);
bool replaceNamedDeclName(const clang::NamedDecl *ND,
const std::string &NameStr);
bool replaceCXXDtorCallExpr(const clang::CXXMemberCallExpr *CE,
std::string &Name);
bool removeSpecifier(clang::NestedNameSpecifierLoc Loc);
bool replaceSpecifier(clang::NestedNameSpecifierLoc Loc,
const std::string &Name);
void getQualifierAsString(clang::NestedNameSpecifierLoc Loc,
std::string &Str);
void getSpecifierAsString(clang::NestedNameSpecifierLoc Loc,
std::string &Str);
bool replaceRecordType(clang::RecordTypeLoc &RTLoc, const std::string &Name);
bool isSingleDecl(const clang::VarDecl *VD);
bool isTheFirstDecl(const clang::VarDecl *VD);
bool removeTextFromLeftAt(clang::SourceRange Range, char C,
clang::SourceLocation EndLoc);
clang::SourceLocation getLocationFromLeftUntil(clang::SourceLocation StartLoc,
char C);
bool removeTextUntil(clang::SourceRange Range, char C);
bool replaceCXXDestructorDeclName(const clang::CXXDestructorDecl *DtorDecl,
const std::string &Name);
bool removeCXXCtorInitializer(const clang::CXXCtorInitializer *Init,
unsigned Index, unsigned NumInits);
bool removeClassDecls(const clang::CXXRecordDecl *CXXRD);
bool removeClassTemplateDecls(const clang::ClassTemplateDecl *TmplD);
bool replaceCXXMethodNameAfterQualifier(
const clang::NestedNameSpecifierLoc *QualLoc,
const clang::CXXMethodDecl *MD,
const std::string &NewName);
private:
static RewriteUtils *Instance;
static const char *TmpVarNamePrefix;
clang::Rewriter *TheRewriter;
clang::SourceManager *SrcManager;
RewriteUtils(void)
: TheRewriter(NULL),
SrcManager(NULL)
{ }
~RewriteUtils(void) { }
int getOffsetUntil(const char *Buf, char Symbol);
int getSkippingOffset(const char *Buf, char Symbol);
clang::SourceLocation getEndLocationAfter(clang::SourceRange Range,
char Symbol);
clang::SourceLocation getLocationAfterSkiping(clang::SourceLocation StartLoc,
char Symbol);
unsigned getLocationOffsetAndFileID(clang::SourceLocation Loc,
clang::FileID &FID,
clang::SourceManager *SrcManager);
clang::SourceLocation getVarDeclTypeLocEnd(const clang::VarDecl *VD);
clang::SourceLocation getVarDeclTypeLocBegin(const clang::VarDecl *VD);
clang::SourceLocation
getParamSubstringLocation(clang::SourceLocation StartLoc, size_t Size,
const std::string &Substr);
void indentAfterNewLine(llvm::StringRef Str,
std::string &NewStr,
const std::string &IndentStr);
unsigned getOffsetBetweenLocations(clang::SourceLocation StartLoc,
clang::SourceLocation EndLoc,
clang::SourceManager *SrcManager);
clang::SourceLocation skipPossibleTypeRange(const clang::Type *Ty,
clang::SourceLocation OrigEndLoc,
clang::SourceLocation VarStartLoc);
void skipRangeByType(const std::string &BufStr,
const clang::Type *Ty, int &Offset);
bool removeArgFromExpr(const clang::Expr *E, int ParamPos);
const clang::Expr *getArgWrapper(const clang::Expr *E, int ParamPos);
unsigned getNumArgsWrapper(const clang::Expr *E);
clang::SourceLocation getExpansionEndLoc(clang::SourceLocation EndLoc);
// Unimplemented
RewriteUtils(const RewriteUtils &);
void operator=(const RewriteUtils &);
};
#endif