forked from amboar/tceetree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathouttree.c
418 lines (355 loc) · 9.5 KB
/
outtree.c
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
/*
* This source code is released for free distribution under the terms of the MIT
* License (MIT):
*
* Copyright (c) 2014, Fabio Visona'
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#ifndef _ALL_IN_ONE
#include "defines.h"
#include "outgraphviz.h"
#include "outtree.h"
#endif // _ALL_IN_ONE
#define ROOTMARK \
0x100 // special value used to find the path to be highlighted (-p
// option)
// start output
int outopen(ttree_t *ptree, treeparam_t *pparam)
{
int iErr = 0;
switch (pparam->outtype) {
case TREEOUT_GRAPHVIZ:
iErr = outopen_gra(ptree, pparam);
break;
default:
iErr = -1;
break;
}
return iErr;
}
// end output
int outclose(ttree_t *ptree, treeparam_t *pparam)
{
int iErr = 0;
switch (pparam->outtype) {
case TREEOUT_GRAPHVIZ:
iErr = outclose_gra(ptree, pparam);
break;
default:
iErr = -1;
break;
}
return iErr;
}
// output one node
int outnode(ttreenode_t *pnode, treeparam_t *pparam, int colr)
{
int iErr = 0;
if (pnode && pnode->outdone)
return iErr;
if (colr >= 0) {
if (colr == ROOTMARK || pnode->icolor == ROOTMARK) {
// assign color only while "rootmarking" or if node was
// previously
// "rootmarked"
pnode->icolor = colr;
}
} else {
if (pnode->icolor == ROOTMARK)
pnode->icolor =
0; // Only "rootmarked", use default color/style
// output node
switch (pparam->outtype) {
case TREEOUT_GRAPHVIZ:
iErr = outnode_gra(pnode, pparam);
break;
default:
iErr = -1;
break;
}
}
pnode->outdone = 1;
return iErr;
}
// output one branch
int outbranch(ttreebranch_t *pbranch, treeparam_t *pparam, int colr)
{
int iErr = 0;
int i;
if (!pbranch)
return -1;
if (pbranch && pbranch->outdone)
return iErr;
// check if branch should be excluded
for (i = 0; i < pparam->excludfno; i++) {
// check if library excluded
if (strcmp(pparam->excludf[i], TT_LIBRARY) == 0 &&
pbranch->child.node->filename == NULL)
return iErr;
// check if caller or callee function name matches one of those
// in the
// exclusion list
if (strcmp(pbranch->parent.node->funname, pparam->excludf[i]) == 0 ||
strcmp(pbranch->child.node->funname, pparam->excludf[i]) == 0)
return iErr;
}
if (colr >= 0) {
if (colr == ROOTMARK || pbranch->icolor == ROOTMARK) {
// assign color only while "rootmarking" or if branch
// was previously
// "rootmarked"
pbranch->icolor = colr;
}
} else {
if (pbranch->icolor == ROOTMARK)
pbranch->icolor =
0; // Only "rootmarked", use default color/style
// output branch
switch (pparam->outtype) {
case TREEOUT_GRAPHVIZ:
iErr = outbranch_gra(pbranch, pparam);
break;
default:
iErr = -1;
break;
}
}
pbranch->outdone = 1;
return iErr;
}
// output of a subtree (forward and backward) starting from pnode
int outsubtree(ttree_t *ptree, treeparam_t *pparam, ttreenode_t *pnode,
int fdepth, int bdepth, int colr)
{
int iErr = 0;
ttreebranch_t *pbranch;
int i, prevcol;
if (pnode == NULL)
return iErr;
if (pnode->subtreeoutdone)
return iErr;
for (i = 0; i < pparam->excludfno; i++) {
// check if library excluded
if (strcmp(pparam->excludf[i], TT_LIBRARY) == 0 &&
pnode->filename == NULL)
return iErr;
// check if node function name matches one of those in the
// exclusion list
if (strcmp(pnode->funname, pparam->excludf[i]) == 0)
return iErr;
}
// before scanning subtree, output the node itself
iErr = outnode(pnode, pparam, colr);
if (colr > 0) {
if (colr == ROOTMARK) {
// if "rootmarking" (colr == ROOTMARK) and the last
// function of the path
// to be highlighted from roots has been found, then
// stop scanning tree
if (pparam->callp)
if (strcmp(pnode->funname, pparam->callp) == 0)
return iErr;
} else {
// if coloring and this is a root, then stop scanning
// tree
if (pnode->isroot)
return iErr;
}
}
if (iErr == 0 && fdepth != 0) {
// forward (children) scanning
if (fdepth > 0)
fdepth--;
pbranch = NULL;
do {
// find all branches starting from this node
pbranch = ttreefindbranch(ptree, pnode, NULL,
pnode->filename, pbranch);
if (pbranch) {
if (!pbranch->outdone) {
// if branch not done
prevcol = pbranch->icolor;
// output branch
iErr = outbranch(pbranch, pparam, colr);
if (iErr != 0)
break;
// do subtree
if (pbranch->child.node !=
pnode) // avoid involving recursion
// in depth decrease
if (colr <= 0 ||
colr == ROOTMARK ||
prevcol == ROOTMARK)
iErr = outsubtree(
ptree, pparam,
pbranch->child.node,
fdepth, 0, colr);
}
}
} while (pbranch != NULL);
}
if (iErr == 0 && bdepth != 0) {
if (bdepth > 0)
bdepth--;
pbranch = NULL;
do {
// find all branches with this node as destination
pbranch =
ttreefindbranch(ptree, NULL, pnode, NULL, pbranch);
if (pbranch) {
if (!pbranch->outdone) {
// if branch not done
prevcol = pbranch->icolor;
// output branch
iErr = outbranch(pbranch, pparam, colr);
if (iErr != 0)
break;
// do subtree
if (pbranch->parent.node !=
pnode) // avoid involving recursion
// in depth decrease
if (colr <= 0 ||
colr == ROOTMARK ||
prevcol == ROOTMARK)
iErr = outsubtree(
ptree, pparam,
pbranch->parent.node,
0, bdepth, colr);
}
}
} while (pbranch != NULL);
}
pnode->subtreeoutdone = 1;
return iErr;
}
// tree output init
void outtreeinit(ttree_t *ptree, int color, int resetroot)
{
ttreenode_t *pnode;
ttreebranch_t *pbranch;
pnode = ptree->firstnode;
while (pnode != NULL) {
pnode->outdone = 0;
pnode->subtreeoutdone = 0;
if (resetroot)
pnode->isroot = 0;
if (color >= 0)
pnode->icolor = color;
pnode = pnode->next;
}
pbranch = ptree->firstbranch;
while (pbranch != NULL) {
pbranch->outdone = 0;
if (color >= 0)
pbranch->icolor = color;
pbranch = pbranch->next;
}
}
// make tree output
int outtree(ttree_t *ptree, treeparam_t *pparam)
{
int iErr = 0, iErrC;
ttreenode_t *pnode;
int i = 0;
if (pparam->verbose)
printf("\nMaking output... ");
// start output
iErr = outopen(ptree, pparam);
if (iErr == 0) {
// init, color = 0, reset root flags
outtreeinit(ptree, 0, 1);
// find all roots and set corresponding isroot flag
for (i = 0; i < pparam->rootno; i++) {
pnode = ptree->firstnode;
while (pnode != NULL) {
if (strcmp(pnode->funname, pparam->root[i]) ==
0) {
pnode->isroot = 1;
// break; break missing because the same
// function can have multiple
// definitions in different files
}
pnode = pnode->next;
}
}
// mark all nodes found scanning the tree starting from the
// specified root
// functions
pnode = ptree->firstnode;
while (iErr == 0 && pnode != NULL) {
if (pnode->isroot) {
// init, don't set color, don't reset isroot
// flags
outtreeinit(ptree, -1, 0);
iErr = outsubtree(ptree, pparam, pnode,
pparam->fdepth,
pparam->bdepth, ROOTMARK);
}
pnode = pnode->next;
}
if (pparam->callp) {
// if an highlight path has been specified
pnode = ptree->firstnode;
while (iErr == 0 && pnode != NULL) {
if (strcmp(pnode->funname, pparam->callp) ==
0) {
// found the last function of path
// init, don't set color, don't reset
// isroot flags
outtreeinit(ptree, -1, 0);
// bdepth and fdepth are inverted on
// purpose, because we are scanning
// toward roots
// in the opposite direction
// last parameter = 1 because we are
// coloring the path
iErr = outsubtree(ptree, pparam, pnode,
pparam->bdepth,
pparam->fdepth, 1);
// break; break missing because the same
// function can have multiple
// defintions in different files
}
pnode = pnode->next;
}
}
// init, don't set color, don't reset isroot flags
outtreeinit(ptree, -1, 0);
// this is the actual output production
pnode = ptree->firstnode;
while (iErr == 0 && pnode != NULL) {
// find all roots and start scanning from those nodes
if (pnode->isroot)
iErr = outsubtree(ptree, pparam, pnode,
pparam->fdepth,
pparam->bdepth, -1);
pnode = pnode->next;
}
// terminate the tree output
iErrC = outclose(ptree, pparam);
if (iErr == 0)
iErr = iErrC;
}
if (pparam->verbose)
printf("done\n");
return iErr;
}