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
|
/* This file is part of GNU cflow
Copyright (C) 1997, 2005, 2007, 2009, 2010, 2011 Sergey Poznyakoff
GNU cflow is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
GNU cflow is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with GNU cflow; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#include <cflow.h>
#include <parser.h>
unsigned char *level_mark;
/* Tree level information. level_mark[i] contains 1 if there are more
* leafs on the level `i', otherwise it contains 0
*/
int level_mark_size = 0; /* Actual size of level_mark */
int level_mark_incr = 128; /* level_mark is expanded by this number of bytes */
int out_line = 1; /* Current output line number */
FILE *outfile; /* Output file */
static void
set_level_mark(int lev, int mark)
{
if (lev >= level_mark_size) {
level_mark_size += level_mark_incr;
level_mark = xrealloc(level_mark, level_mark_size);
}
level_mark[lev] = mark;
}
/* Print current tree level
*/
void
print_level(int lev, int last)
{
int i;
if (print_line_numbers)
fprintf(outfile, "%5d ", out_line);
if (print_levels)
fprintf(outfile, "{%4d} ", lev);
fprintf(outfile, "%s", level_begin);
for (i = 0; i < lev; i++)
fprintf(outfile, "%s", level_indent[ level_mark[i] ]);
fprintf(outfile, "%s", level_end[last]);
}
/* Low level output functions */
struct output_driver {
char *name;
int (*handler) (cflow_output_command cmd,
FILE *outfile, int line,
void *data, void *handler_data);
void *handler_data;
};
static int driver_index;
static int driver_max=0;
struct output_driver output_driver[MAX_OUTPUT_DRIVERS];
int
register_output(const char *name,
int (*handler) (cflow_output_command cmd,
FILE *outfile, int line,
void *data, void *handler_data),
void *handler_data)
{
if (driver_max == MAX_OUTPUT_DRIVERS-1)
abort ();
output_driver[driver_max].name = strdup(name);
output_driver[driver_max].handler = handler;
output_driver[driver_max].handler_data = handler_data;
return driver_max++;
}
int
select_output_driver(const char *name)
{
int i;
for (i = 0; i < driver_max; i++)
if (strcmp(output_driver[i].name, name) == 0) {
driver_index = i;
return 0;
}
return -1;
}
void
output_init()
{
output_driver[driver_index].handler(cflow_output_init,
NULL, 0,
NULL,
output_driver[driver_index].handler_data);
}
void
newline()
{
output_driver[driver_index].handler(cflow_output_newline,
outfile, out_line,
NULL,
output_driver[driver_index].handler_data);
out_line++;
}
static void
begin()
{
output_driver[driver_index].handler(cflow_output_begin,
outfile, out_line,
NULL,
output_driver[driver_index].handler_data);
}
static void
end()
{
output_driver[driver_index].handler(cflow_output_end,
outfile, out_line,
NULL,
output_driver[driver_index].handler_data);
}
static void
separator()
{
output_driver[driver_index].handler(cflow_output_separator,
outfile, out_line,
NULL,
output_driver[driver_index].handler_data);
}
#if 0
static void
print_text(char *buf)
{
output_driver[driver_index].handler(cflow_output_text,
outfile, out_line,
buf,
output_driver[driver_index].handler_data);
}
#endif
static int
print_symbol (int direct, int level, int last, Symbol *sym)
{
struct output_symbol output_symbol;
output_symbol.direct = direct;
output_symbol.level = level;
output_symbol.last = last;
output_symbol.sym = sym;
return output_driver[driver_index].handler(cflow_output_symbol,
outfile, out_line,
&output_symbol,
output_driver[driver_index].handler_data);
}
static int
compare(const void *ap, const void *bp)
{
Symbol * const *a = ap;
Symbol * const *b = bp;
return strcmp((*a)->name, (*b)->name);
}
static int
is_var(Symbol *symp)
{
if (include_symbol(symp)) {
if (symp->type == SymIdentifier)
return symp->storage == ExternStorage ||
symp->storage == StaticStorage;
else
return 1;
}
return 0;
}
int
symbol_is_function(Symbol *symp)
{
return symp->type == SymIdentifier && symp->arity >= 0;
}
static void
clear_active(Symbol *sym)
{
sym->active = 0;
}
/* Cross-reference output */
void
print_refs(char *name, struct linked_list *reflist)
{
Ref *refptr;
struct linked_list_entry *p;
for (p = linked_list_head(reflist); p; p = p->next) {
refptr = (Ref*)p->data;
fprintf(outfile, "%s %s:%d\n",
name,
refptr->source,
refptr->line);
}
}
static void
print_function(Symbol *symp)
{
if (symp->source) {
fprintf(outfile, "%s * %s:%d %s\n",
symp->name,
symp->source,
symp->def_line,
symp->decl);
}
print_refs(symp->name, symp->ref_line);
}
static void
print_type(Symbol *symp)
{
if (symp->source)
fprintf(outfile, "%s t %s:%d\n",
symp->name,
symp->source,
symp->def_line);
}
void
xref_output()
{
Symbol **symbols, *symp;
size_t i, num;
num = collect_symbols(&symbols, is_var, 0);
qsort(symbols, num, sizeof(*symbols), compare);
/* produce xref output */
for (i = 0; i < num; i++) {
symp = symbols[i];
switch (symp->type) {
case SymIdentifier:
print_function(symp);
break;
case SymToken:
print_type(symp);
break;
case SymUndefined:
break;
}
}
free(symbols);
}
/* Tree output */
static void
set_active(Symbol *sym)
{
sym->active = out_line;
}
static int
is_printable(struct linked_list_entry *p)
{
return p != NULL && include_symbol((Symbol*)p->data);
}
static int
is_last(struct linked_list_entry *p)
{
while ((p = p->next))
if (is_printable(p))
return 0;
return 1;
}
/* Produce direct call tree output
*/
static void
direct_tree(int lev, int last, Symbol *sym)
{
struct linked_list_entry *p;
int rc;
if (sym->type == SymUndefined
|| (max_depth && lev >= max_depth)
|| !include_symbol(sym))
return;
rc = print_symbol(1, lev, last, sym);
newline();
if (rc || sym->active)
return;
set_active(sym);
for (p = linked_list_head(sym->callee); p; p = p->next) {
set_level_mark(lev+1, !is_last(p));
direct_tree(lev+1, is_last(p), (Symbol*)p->data);
}
clear_active(sym);
}
/* Produce reverse call tree output
*/
static void
inverted_tree(int lev, int last, Symbol *sym)
{
struct linked_list_entry *p;
int rc;
if (sym->type == SymUndefined
|| (max_depth && lev >= max_depth)
|| !include_symbol(sym))
return;
rc = print_symbol(0, lev, last, sym);
newline();
if (rc || sym->active)
return;
set_active(sym);
for (p = linked_list_head(sym->caller); p; p = p->next) {
set_level_mark(lev+1, !is_last(p));
inverted_tree(lev+1, is_last(p), (Symbol*)p->data);
}
clear_active(sym);
}
static void
tree_output()
{
Symbol **symbols, *main_sym;
size_t i, num;
cflow_depmap_t depmap;
/* Collect functions and assign them ordinal numbers */
num = collect_functions(&symbols);
for (i = 0; i < num; i++)
symbols[i]->ord = i;
/* Create a dependency matrix */
depmap = depmap_alloc(num);
for (i = 0; i < num; i++) {
if (symbols[i]->callee) {
struct linked_list_entry *p;
for (p = linked_list_head(symbols[i]->callee); p;
p = p->next) {
Symbol *s = (Symbol*) p->data;
if (symbol_is_function(s))
depmap_set(depmap, i, ((Symbol*)p->data)->ord);
}
}
}
depmap_tc(depmap);
/* Mark recursive calls */
for (i = 0; i < num; i++)
if (depmap_isset(depmap, i, i))
symbols[i]->recursive = 1;
free(depmap);
free(symbols);
/* Collect and sort all symbols */
num = collect_symbols(&symbols, is_var, 0);
qsort(symbols, num, sizeof(*symbols), compare);
/* Produce output */
begin();
if (reverse_tree) {
for (i = 0; i < num; i++) {
inverted_tree(0, 0, symbols[i]);
separator();
}
} else {
main_sym = lookup(start_name);
if (main_sym) {
direct_tree(0, 0, main_sym);
separator();
} else {
for (i = 0; i < num; i++) {
if (symbols[i]->callee == NULL)
continue;
direct_tree(0, 0, symbols[i]);
separator();
}
}
}
end();
free(symbols);
}
void
output()
{
if (strcmp(outname, "-") == 0) {
outfile = stdout;
} else {
outfile = fopen(outname, "w");
if (!outfile)
error(2, errno, _("cannot open file `%s'"), outname);
}
set_level_mark(0, 0);
if (print_option & PRINT_XREF) {
xref_output();
}
if (print_option & PRINT_TREE) {
tree_output();
}
fclose(outfile);
}
|