-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.js
More file actions
168 lines (140 loc) · 4.18 KB
/
Copy pathdraw.js
File metadata and controls
168 lines (140 loc) · 4.18 KB
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
var fs = require ('fs');
var esprima = require ('esprima');
function traverse (node, func) {
func (node);
for (var key in node) {
if (node.hasOwnProperty (key)) {
var child = node[key];
if (typeof child === 'object' && child !== null) {
if (Array.isArray(child)) {
child.forEach (function(node) {
traverse (node, func);
});
} else {
traverse (child, func);
}
}
}
}
}
function tojson (ast) {
return JSON.stringify (ast, null, 2);
}
function loc2str (loc) {
return "<" + loc.start.line + "c" + loc.start.column + ">";
}
// naming convention for anonymous functions
function nameOfLambda (fun) {
return (loc2str(fun.loc));
}
function nameOfFunction (fun) {
if (fun.id === null) // anonymous function
return nameOfLambda (fun);
else
return fun.id.name;
}
var callgraph = [];
function visitExp (fname,exp) {
if (exp.type == "CallExpression") {
if (exp.callee.type == "Identifier") {
// console.log (fname + " -> " + exp.callee.name);
callgraph.push({caller : fname, callee : exp.callee.name});
}
}
}
function visitIfStmt (fname,ifstmt) {
visitExp (fname,ifstmt.test);
visitStmt (fname,ifstmt.consequent);
if (ifstmt.alternate !== null)
visitStmt (fname,ifstmt.alternate);
}
function visitLabStmt (fname,labstmt) {
visitStmt (fname,labstmt.body);
}
function visitWithStmt (fname,withstmt) {
visitStmt (fname,withStmt.body);
}
function visitRetStmt (fname,retstmt) {
if (retstmt.argument !== null)
visitExp (fname,retstmt.argument);
}
function visitThrowStmt (fname,throwstmt) {
visitExp (fname,throwstmt.argument);
}
function visitTryStmt (fname,trystmt) {
visitBlockStmt (fname,trystmt.block);
if (trystmt.finalizer !== null)
visitBlockStmt (fname,trystmt.finalizer);
}
function visitWhileStmt (fname,whilestmt) {
visitExp (fname,whilestmt.test);
visitStmt (fname,whilestmt.body);
}
function visitDoWhileStmt (fname,dowhilestmt) {
visitStmt (fname,dowhilestmt.body);
visitStmt (fname,dowhilestmt.test);
}
function visitForStmt (fname,forstmt) {
// ignored the "init" field
if (forstmt.test !== null)
visitExp (fname,forstmt.test);
if (forstmt.update !== null)
visitExp (fname,forstmt.update);
visitStmt (fname,forstmt.body);
}
function visitForInStmt (fname,stmt) {
// ignored stmt.left
visitExp (fname,stmt.right);
visitStmt (fname,stmt.body);
}
function visitExpStmt (fname,stmt) {
visitExp (fname,stmt.expression);
}
function visitBlockStmt (fname,bstmt) {
bstmt.body.forEach (function (stmt) {
visitStmt (fname,stmt);
});
}
function visitStmt (fname,stmt) {
if (stmt.type == "BlockStatement") visitBlockStmt (fname,stmt);
if (stmt.type == "IfStatement") visitIfStmt (fname,stmt);
if (stmt.type == "LabeledStatement") visitLabStmt (fname,stmt);
if (stmt.type == "WithStatement") visitWithStmt (fname,stmt);
if (stmt.type == "ReturnStatement") visitRetStmt (fname,stmt);
if (stmt.type == "TryStatement") visitTryStmt (fname,stmt);
if (stmt.type == "TryStatement") visitTryStmt (fname,stmt);
if (stmt.type == "WhileStatement") visitWhileStmt (fname,stmt);
if (stmt.type == "DoWhileStatement") visitDoWhileStmt (fname,stmt);
if (stmt.type == "ForStatement") visitForStmt (fname,stmt);
if (stmt.type == "ForInStatement") visitForInStmt (fname,stmt);
if (stmt.type == "ExpressionStatement") visitExpStmt (fname,stmt);
}
function visitFuntion (fname,fun) {
visitBlockStmt (fname,fun.body);
}
function visitNode (node) {
if (node.type == "FunctionDeclaration" ||
node.type == "FunctionExpression") {
visitFuntion (nameOfFunction(node),node);
}
}
function analyzeCallGraph (code) {
var ast = esprima.parse (code, {loc : true});
// console.log (tojson (ast));
traverse (ast, visitNode);
}
if (process.argv.length < 3) {
console.log ('Usage: analyze.js file.js');
process.exit(1);
}
var filename = process.argv[2];
var code = fs.readFileSync (filename);
analyzeCallGraph (code);
function printInDot (callgraph) {
console.log ("digraph { ");
callgraph.forEach (function (call) {
console.log (call.caller + " -> " + call.callee + ";");
});
console.log ("}");
}
printInDot (callgraph);