-
Notifications
You must be signed in to change notification settings - Fork 4
/
call_graph.js
357 lines (313 loc) · 11 KB
/
call_graph.js
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
/**
* @author Kishan Nirghin
*
* @description Callgraph of functions relations
* The extendible callgraph that should represent all functioncalls of a project
* The nodes: being the functions
* and edges: caller-callee relationships
*
* The rootNodes are nodes that are autoexecuted at the start
* for now these are the files that contain the JS functions.
* e.g. whenever a function is executed from a JS file, the function will become
* a node, and that file that triggered the execution will become a rootNode.
*
* The following params are used throughout this file
* @param functionData {
* file: {String},
* range: [{Number}, {Number}]
* }
* thus all data that represent a function. Note that for rootNodes the ranges
* are both null.
*
* @param stripObject {Boolean}
* To deal with circular structures (which any graph probably suffers from),
* the strip option will remove any references to other objects, and rather
* uses the raw data.
*/
const { Dotify, objectToDOT } = require("./dotify");
const logger = require("./_logger");
const Node = require("./call_graph-node");
module.exports = class CallGraph {
constructor(functions) {
this.nodes = [];
this.edges = [];
/* Note: these aren't functions, rather the files that call functions */
this.rootNodes = [];
functions.forEach((functionData) => {
this.addNode(functionData);
})
}
/**
* Creates a new node and adds it to the callGraph
* Also creates a rootNode if necessary
*/
addNode(functionData) {
if (!this.fitsNode(functionData)) {
logger.warn("[addNode] adding invalid node", functionData);
}
this.nodes.push(new Node(functionData));
/* Check if a rootNode should be added */
var rootNode = { file: functionData.file, type: 'rootNode', range: [null, null] };
if (!this.getRootNode(rootNode)) {
this.rootNodes.push(new Node(rootNode));
}
}
/**
* Adds a valid rootNode based on the functionData
*/
addRootNode(functionData, stripObject) {
var rootNode = this.getRootNode(functionData);
if (rootNode) {
logger.warn("[addRootNode] already exists", functionData);
}
else {
rootNode = new Node({ file: functionData.file, range: [null, null] });
this.rootNodes.push(rootNode);
}
return stripObject ? rootNode.functionData : rootNode;
}
/**
* Creates a new alive node with an unknown caller
*/
addAliveNode(functionData, analyzer) {
var rootNodeFunctionData = { file: functionData.file, range: [null, null] };
var edge = this.addEdge(rootNodeFunctionData, functionData, analyzer, true);
return edge;
}
/**
* Wrapper function to add an edge to a node
* It fetches both nodes a adds a newly created edge to the caller
*
* NOTABLE FIX - a bit sloppy but: when an edge is added from a rootNode
* there is a change that this rootNode does not exist (yet). Therefore
* check if the caller is a rootNode and add if it doesn't yet exist
*
* (TODO better fix: create all rootNodes at the start - thus for every
* involved file)
*/
addEdge(functionDataCaller, functionDataCallee, analyzer, stripObject) {
if (this.fitsRootNode(functionDataCaller) && !this.getRootNode(functionDataCaller)) {
this.rootNodes.push(new Node(functionDataCaller));
}
var caller = this.getNode(functionDataCaller);
var callee = this.getNode(functionDataCallee);
if (!caller || !callee) { return null; }
return caller.addEdge(callee, analyzer, stripObject);
}
/**
* Fetches an existing nodeObject given its functionData
*/
getNode(functionData, excludeRootNodes) {
var nodeList = this.nodes;
if (!this.fitsNode(functionData)) {
if (!(this.fitsRootNode(functionData))) {
logger.warn("[getNode] Invalid node", functionData);
return null;
}
if (excludeRootNodes) {
logger.warn("[getNode] rootNodes excluded", functionData);
return null;
}
/* continue with rootNodes */
nodeList = this.rootNodes;
}
for (var i = 0; i < nodeList.length; i++){
var node = nodeList[i];
if (node.functionData.file == functionData.file &&
node.functionData.range[0] == functionData.range[0] &&
node.functionData.range[1] == functionData.range[1]) {
return node;
}
};
logger.warn("[getNode] not found", functionData);
return null;
}
/**
* Should only be getting rootNodes
*/
getRootNode(functionData, stripObject) {
if (!this.fitsRootNode(functionData)) {
logger.warn("[getRootNode] invalid node", functionData);
}
for (var i = 0; i < this.rootNodes.length; i++){
if (this.rootNodes[i].functionData.file == functionData.file) {
var rootNode = this.rootNodes[i];
return stripObject ? rootNode.functionData : rootNode;
}
}
return null;
}
/**
* Fetches a rootNode, creates it if it didn't exist yet.
*/
assertRootNode(functionData, stripObject) {
var rootNode = this.getRootNode(functionData, stripObject);
if (!rootNode) {
logger.info("Creating rootnode", functionData);
rootNode = this.addRootNode(functionData, stripObject);
}
return rootNode;
}
/**
* If some functionData fits the rootNode constraints
*/
fitsRootNode(functionData) {
if (!functionData) { return false; }
return (functionData.range[0] == null && functionData.range[0] == null)
}
/**
* If some functionData fits the node constraints.
*/
fitsNode(functionData) {
if (!functionData) { return false; }
return (functionData.range[0] != null && functionData.range[1] != null);
}
/**
* Boolean on whether a node exists
*/
nodeExists(functionData) {
if (!this.fitsNode(functionData)) {
logger.warn("[nodeExists] Invalid node", functionData);
return false;
}
this.nodes.forEach((node) => {
if (node.functionData.file == functionData.file &&
node.functionData.range[0] == functionData.range[0] &&
node.functionData.range[1] == functionData.range[1]) {
return node;
}
});
return false;
}
/**
* Prints the callgraph in a pseudo readable/interpretable format
*/
print() {
this.rootNodes.forEach((node) => {
node.printGraph(0);
});
}
/**
* Gets the DOT representation of the current callgraph
*/
getDOT() {
var dotty = new Dotify("digraph", "lacunaCG");
/* the expanding list of considered nodes => prevents infinite loops */
var consideredNodes = [];
this.rootNodes.forEach((node) => {
node.addToDotify(dotty, consideredNodes);
});
return dotty.getDOT();
}
/**
* Fetches the nodes that are not called by any other node
* (Dead functions).
*/
getDisconnectedNodes(stripObject) {
var connectedNodes = this.getConnectedNodes(stripObject);
var disconnectedNodes = [];
this.nodes.forEach((node) => {
if (!connectedNodes.includes(node.functionData)) {
disconnectedNodes.push(node.functionData);
}
});
return disconnectedNodes;
}
/**
* Fetches all nodes (except the rootNodes) that have been called
* Starts from the rootNodes and works downwards.
*/
getConnectedNodes(stripObject) {
var connectedNodes = [];
this.rootNodes.forEach((childNode) => {
var node = stripObject ? childNode.functionData : childNode;
if (connectedNodes.includes(node)) { return; }
// if (addRootNodes) { connectedNodes.push(node); }
childNode.addConnectedNodes(connectedNodes, stripObject);
});
return connectedNodes;
}
/**
* Fetches some base stats about the current callgraph
*/
getStatistics() {
var connectedNodes = this.getConnectedNodes(true);
var disconnectedNodes = this.getDisconnectedNodes(true);
return {
connectedNodes: connectedNodes.length,
disconnectedNodes: disconnectedNodes.length,
nodes: this.nodes.length,
files: this.rootNodes.length
}
}
/**
* Getters
*/
getNodes(stripObject) {
var nodes = [];
this.nodes.forEach((node) => {
if (stripObject) { node = node.functionData; }
nodes.push(node);
});
return nodes;
}
getRootNodes(stripObject) {
var rootNodes = [];
this.rootNodes.forEach((node) => {
if (stripObject) { node = node.functionData; }
rootNodes.push(node);
});
return rootNodes;
}
/**
* Converts any kind of nodeData to the expected functionData as
* described above.
*
* Usecase 1: convert start to range (empty on rootNode)
*
* CONVERTS THIS (Doesn't do the file thing yet? see tajs.js)
* { file: ' example/demo.out/fol/script.js',
* start: { line: 18, column: 0 } },
* }
* OR
* { functionName: 'xxo' }
*
* TO THIS
* { file: 'fol/script.js', range: [ null, null ] }
*
*
* Usecase
*/
convertToFunctionData(nodeData) {
var functionData = {};
if (nodeData.hasOwnProperty("functionName")) {
var nodes = this.getNodes(true);
for (var i = 0; i < nodes.length; i++){
if (nodes[i].functionName == nodeData.functionName) {
return { file: nodes[i].file, range: nodes[i].range };
}
}
logger.warn("[convertToFunctionData] invalid functionName ", nodeData.functionName);
return;
}
/* copy file */
if (nodeData.hasOwnProperty("file")) {
functionData.file = nodeData.file;
} else { logger.warn("[convertToFunctionData] invalid nodeData", nodeData); }
/* Fix range based on start */
if (!nodeData.hasOwnProperty("range") && nodeData.hasOwnProperty("start")) {
var nodes = this.getNodes(true);
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (node.start.line == nodeData.start.line && node.start.column == nodeData.start.column) {
functionData.range = node.range;
break;
}
}
}
if (!functionData.hasOwnProperty("range")) {
functionData.range = [null, null]; /* rootNode */
}
return functionData;
}
}