-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharg_patterns_sample.cpp
441 lines (345 loc) · 14.6 KB
/
arg_patterns_sample.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Code started by editing tooling_sample.cpp from LLVM/Clang distribution,
// by Eli Bendersky ([email protected]), public domain
#include <sstream>
#include <string>
#include <unordered_set>
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "llvm/Support/raw_ostream.h"
#include "dofunc.h"
using namespace clang;
using namespace clang::driver;
using namespace clang::tooling;
static llvm::cl::OptionCategory ToolingSampleCategory("Do-functions Args Patterns Detector");
const bool DEBUG = false;
const bool DUMP = false;
// By implementing RecursiveASTVisitor, we can specify which AST nodes
// we're interested in by overriding relevant methods.
typedef std::unordered_set<DeclRefExpr*> KnownAccessesTy;
class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor> {
public:
MyASTVisitor(Rewriter &R) : TheRewriter(R) {}
std::string printLoc(SourceLocation l) {
return l.printToString(TheRewriter.getSourceMgr());
}
Stmt* skipParen(Stmt *s) {
if (ParenExpr *p = dyn_cast<ParenExpr>(s)) {
return p->getSubExpr();
}
return s;
}
bool isListFieldAccess(Stmt*& stmt, FieldDecl *fd) { // advances stmt
// `-ParenExpr 0x2b48120 <line:390:17, col:39> 'struct SEXPREC *' lvalue <--------------------------------- the CAR starts here
// `-MemberExpr 0x2b480f0 <col:18, col:33> 'struct SEXPREC *' lvalue .carval 0x29115e0
// `-MemberExpr 0x2b480c0 <col:18, col:25> 'struct listsxp_struct':'struct listsxp_struct' lvalue .listsxp 0x2917db0
// `-MemberExpr 0x2b48090 <col:18, col:23> 'union (anonymous union at ../../src/include/Rinternals.h:267:5)':'union SEXPREC::(anonymous at ../../sr
// `-ImplicitCastExpr 0x2b48078 <col:18, col:20> 'struct SEXPREC *' <LValueToRValue>
// `-ParenExpr 0x2b48058 <col:18, col:20> 'struct SEXPREC *' lvalue
Stmt *s = stmt;
s = skipParen(s);
if (MemberExpr *m = dyn_cast<MemberExpr>(s)) {
if (!m->isLValue() || m->isArrow()) {
return false;
}
if (m->getMemberDecl() != fd) {
return false;
}
s = m->getBase();
} else {
return false;
}
s = skipParen(s);
if (MemberExpr *m = dyn_cast<MemberExpr>(s)) {
if (!m->isLValue() || m->isArrow()) {
return false;
}
if (m->getMemberDecl() != sexpRecListSxpFieldDecl) {
return false;
}
s = m->getBase();
} else {
return false;
}
s = skipParen(s);
if (MemberExpr *m = dyn_cast<MemberExpr>(s)) {
if (!m->isLValue() || !m->isArrow()) {
return false;
}
if (m->getMemberDecl() != sexpRecUnionFieldDecl) {
return false;
}
s = m->getBase();
} else {
return false;
}
s = skipParen(s);
if (ImplicitCastExpr *ic = dyn_cast<ImplicitCastExpr>(s)) {
s = ic->getSubExprAsWritten();
}
s = skipParen(s);
stmt = s;
return true;
}
// detect list accesses of form CA<ncars>R(CD<ncdrs>R(var))
// where ncars > 0 || ncdrs > 0
bool isListAccess(Stmt *s, unsigned& ncars, unsigned& ncdrs, VarDecl*& var, SourceLocation& loc) {
// declaration references (nodes) of already detected list accesses
// this is used to avoid multiple detection for the same complex access, e.g.
// with CADR(x), we don't want to detect also CDR(x)
unsigned _ncars = 0;
while (isListFieldAccess(s, listSxpCarFieldDecl)) {
_ncars++;
}
unsigned _ncdrs = 0;
while (isListFieldAccess(s, listSxpCdrFieldDecl)) {
_ncdrs++;
}
if (_ncars > 0 || _ncdrs > 0) {
if (DeclRefExpr *d = dyn_cast<DeclRefExpr>(s)) {
if (VarDecl *v = dyn_cast<VarDecl>(d->getDecl())) {
auto dsearch = knownAccesses.find(d);
if (dsearch == knownAccesses.end()) {
knownAccesses.insert(d);
ncars = _ncars;
ncdrs = _ncdrs;
var = v;
loc = d->getLocStart();
return true;
}
}
}
}
return false;
}
bool isListAccess(Stmt *s, ListAccess& la) {
unsigned ncars, ncdrs;
VarDecl* var;
SourceLocation loc;
if (!isListAccess(s, ncars, ncdrs, var, loc)) {
return false;
}
if (ncars != 1) { // ListAccess only supports CADnR accesses
return false;
}
la.varName = var->getNameAsString();
la.isArgsVar = (var == argsDecl);
la.line = TheRewriter.getSourceMgr().getExpansionLineNumber(loc);
la.ncdrs = ncdrs;
return true;
}
bool VisitStmt(Stmt *s) {
if (BinaryOperator *bo = dyn_cast<BinaryOperator>(s)) {
if (bo->isAssignmentOp()) {
if (DeclRefExpr *dr = dyn_cast<DeclRefExpr>(bo->getLHS())) {
if (dr->getDecl() == argsDecl) {
if (DEBUG) llvm::errs() << "Modification of args variable at " << printLoc(bo->getOperatorLoc()) << "\n";
}
}
}
}
if (0) {
unsigned ncars, ncdrs;
VarDecl *var;
SourceLocation loc;
if (isListAccess(s, ncars, ncdrs, var, loc)) {
llvm::errs() << "List access to variable \"" << cast<VarDecl>(var)->getNameAsString() << "\" ncars=" << std::to_string(ncars) << " ncdrs=" <<
std::to_string(ncdrs) << " at " << printLoc(s->getLocStart()) << "\n";
}
}
ListAccess la;
if (isListAccess(s, la)) {
llvm::errs() << "Detected list access " << la.str() << "\n";
}
return true;
}
bool VisitDecl(Decl *d) {
// load declarations needed to parse list accesses
// NOTE: child nodes are visited after the parent nodes of the AST tree
if (FieldDecl *fd = dyn_cast<FieldDecl>(d)) {
if (fd->getNameAsString() == "listsxp" && fd->getParent()->isUnion() && fd->getParent()->getNameAsString() == "") {
RecordDecl *ud = fd->getParent();
if (RecordDecl *up = dyn_cast<RecordDecl>(ud->getParent())) {
if (up == sexpRecDecl && isa<ElaboratedType>(fd->getType())) {
const ElaboratedType *et = dyn_cast<ElaboratedType>(fd->getType());
if (const RecordType *rt = dyn_cast<RecordType>(et->getNamedType().getTypePtr())) {
if (rt->getDecl() == listSxpDecl) {
sexpRecUnionDecl = ud;
if (DEBUG) llvm::errs() << "Detected declaration of anonymous SEXPREC union.\n";
sexpRecListSxpFieldDecl = fd;
if (DEBUG) llvm::errs() << "Detected listsxp field in SEXPREC.\n";
}
}
}
}
}
// NOTE: not checking type of fields
if (fd->getNameAsString() == "carval" && fd->getParent() == listSxpDecl) {
listSxpCarFieldDecl = fd;
if (DEBUG) llvm::errs() << "Detected car field in listsxp.\n";
}
if (fd->getNameAsString() == "cdrval" && fd->getParent() == listSxpDecl) {
listSxpCdrFieldDecl = fd;
if (DEBUG) llvm::errs() << "Detected cdr field in listsxp.\n";
}
if (fd->getNameAsString() == "tagval" && fd->getParent() == listSxpDecl) {
listSxpTagFieldDecl = fd;
if (DEBUG) llvm::errs() << "Detected tag field in listsxp.\n";
}
if (fd->getNameAsString() == "u" && fd->getParent() == sexpRecDecl) {
sexpRecUnionFieldDecl = fd;
if (DEBUG) llvm::errs() << "Detected u field (union) in SEXPREC.\n";
}
}
if (RecordDecl *rd = dyn_cast<RecordDecl>(d)) {
if (rd->getParentFunctionOrMethod() == NULL && rd->isCompleteDefinition() && rd->getLexicalParent()->isTranslationUnit() &&
rd->getNameAsString() == "SEXPREC" && rd->isStruct()) {
sexpRecDecl = rd;
if (DEBUG) llvm::errs() << "Detected declaration of SEXPREC.\n";
return true;
}
if (rd->getParentFunctionOrMethod() == NULL && rd->isCompleteDefinition() && rd->getLexicalParent()->isTranslationUnit() &&
rd->getNameAsString() == "listsxp_struct" && rd->isStruct()) {
listSxpDecl = rd;
if (DEBUG) llvm::errs() << "Detected declaration of listsxp_struct.\n";
return true;
}
}
if (!inDoFunction) {
return true;
}
// Detect shadowing of the args argument by a local variable
// FIXME: this can possibly be removed
if (isa<VarDecl>(d) && !isa<ParmVarDecl>(d)) {
if (cast<VarDecl>(d)->getNameAsString() == argsDecl->getNameAsString()) {
llvm::errs() << "Declaration of local variable \"" << cast<VarDecl>(d)->getNameAsString() << "\" which shadows function argument in function \"" << funName << "\"\n";
inDoFunction = false;
return true;
}
}
return true;
}
bool VisitFunctionDecl(FunctionDecl *f) {
// Only function definitions (with bodies), not declarations.
if (!f->hasBody()) {
inDoFunction = false;
return true;
}
Stmt *FuncBody = f->getBody();
// TODO: perhaps easier to simply check by function name
unsigned nparams = f->param_size();
if (nparams != 4) {
inDoFunction = false;
return true;
}
funName = f->getNameInfo().getName().getAsString();
std::string dopref = "do_";
if (funName.substr(0, dopref.length()) != dopref) {
inDoFunction = false;
return true;
}
for(unsigned i = 0; i < nparams; i++) {
ParmVarDecl *p = f->getParamDecl(i);
QualType t = p->getTypeSourceInfo()->getType();
if (t.getAsString() != "SEXP") {
inDoFunction = false;
return true;
}
}
argsDecl = f->getParamDecl(2);
llvm::errs() << "Function " << funName << " may be a do_XXX function with args argument \"" << argsDecl->getNameAsString() << "\".\n";
inDoFunction = true;
knownAccesses.clear();
return true;
}
private:
Rewriter &TheRewriter;
// RecordDecl 0x1a6b510 <../../src/include/Rinternals.h:220:1, line:224:1> line:220:8 struct listsxp_struct definition
// |-FieldDecl 0x1a6b5e0 <line:221:5, col:21> col:21 carval 'struct SEXPREC *'
// |-FieldDecl 0x1a6b650 <line:222:5, col:21> col:21 cdrval 'struct SEXPREC *'
// `-FieldDecl 0x1a6b6c0 <line:223:5, col:21> col:21 tagval 'struct SEXPREC *
RecordDecl *listSxpDecl = NULL;
FieldDecl *listSxpCarFieldDecl = NULL;
FieldDecl *listSxpCdrFieldDecl = NULL;
FieldDecl *listSxpTagFieldDecl = NULL;
// RecordDecl 0x1a6bd10 prev 0x1a6b280 <../../src/include/Rinternals.h:265:9, line:275:1> line:265:16 struct SEXPREC definition <======= sexpRecDecl
// |-FieldDecl 0x1a6bde0 <line:259:5, col:27> col:27 sxpinfo 'struct sxpinfo_struct':'struct sxpinfo_struct'
// |-FieldDecl 0x1a71a50 <line:260:5, col:21> col:21 attrib 'struct SEXPREC *'
// |-FieldDecl 0x1a71ac0 <line:261:5, col:21> col:21 gengc_next_node 'struct SEXPREC *'
// |-FieldDecl 0x1a71b30 <col:5, col:39> col:39 gengc_prev_node 'struct SEXPREC *'
// |-RecordDecl 0x1a71b80 <line:267:5, line:274:5> line:267:5 union definition <======================================== sexpRecUnionDecl
// | |-FieldDecl 0x1a71c70 <line:268:2, col:24> col:24 primsxp 'struct primsxp_struct':'struct primsxp_struct'
// | |-FieldDecl 0x1a71d10 <line:269:2, col:23> col:23 symsxp 'struct symsxp_struct':'struct symsxp_struct'
// | |-FieldDecl 0x1a71db0 <line:270:2, col:24> col:24 listsxp 'struct listsxp_struct':'struct listsxp_struct' <======== sexpRecListSxpFieldDecl
// | |-FieldDecl 0x1a71e50 <line:271:2, col:23> col:23 envsxp 'struct envsxp_struct':'struct envsxp_struct'
// | |-FieldDecl 0x1a71ef0 <line:272:2, col:23> col:23 closxp 'struct closxp_struct':'struct closxp_struct'
// | `-FieldDecl 0x1a71f90 <line:273:2, col:24> col:24 promsxp 'struct promsxp_struct':'struct promsxp_struct'
// `-FieldDecl 0x1a72070 <line:267:5, line:274:7> col:7 u 'union (anonymous union at ../../src/include/Rinternals.h:267:5)':'union SEXPREC::(anonymous at ../../src/include/Rintern
// <=== sexpRecUnionFieldDecl
RecordDecl *sexpRecDecl = NULL;
RecordDecl *sexpRecUnionDecl = NULL;
FieldDecl *sexpRecListSxpFieldDecl = NULL;
FieldDecl *sexpRecUnionFieldDecl = NULL;
ParmVarDecl *argsDecl = NULL; // the "args" argument of the present do_XXX function
std::string funName;
bool inDoFunction; // FIXME: is this flag needed?
KnownAccessesTy knownAccesses; // already known list accesses
};
// Implementation of the ASTConsumer interface for reading an AST produced
// by the Clang parser.
class MyASTConsumer : public ASTConsumer {
public:
MyASTConsumer(Rewriter &R) : Visitor(R) {}
// declaration.
bool HandleTopLevelDecl(DeclGroupRef DR) override {
for (DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b) {
// Traverse the declaration using our AST visitor.
Visitor.TraverseDecl(*b);
if (DUMP) (*b)->dump();
}
return true;
}
/*
virtual void HandleTranslationUnit(clang::ASTContext &Context) {
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
}
*/
private:
MyASTVisitor Visitor;
};
// For each source file provided to the tool, a new FrontendAction is created.
class MyFrontendAction : public ASTFrontendAction {
public:
MyFrontendAction() {}
void EndSourceFileAction() override {
SourceManager &SM = TheRewriter.getSourceMgr();
llvm::errs() << "** EndSourceFileAction for: "
<< SM.getFileEntryForID(SM.getMainFileID())->getName() << "\n";
// Now emit the rewritten buffer.
TheRewriter.getEditBuffer(SM.getMainFileID()).write(llvm::outs());
}
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef file) override {
llvm::errs() << "** Creating AST consumer for: " << file << "\n";
TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
return llvm::make_unique<MyASTConsumer>(TheRewriter);
}
private:
Rewriter TheRewriter;
};
int main(int argc, const char **argv) {
CommonOptionsParser op(argc, argv, ToolingSampleCategory);
ClangTool Tool(op.getCompilations(), op.getSourcePathList());
// ClangTool::run accepts a FrontendActionFactory, which is then used to
// create new objects implementing the FrontendAction interface. Here we use
// the helper newFrontendActionFactory to create a default factory that will
// return a new MyFrontendAction object every time.
// To further customize this, we could create our own factory class.
return Tool.run(newFrontendActionFactory<MyFrontendAction>().get());
}