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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
|
/* $Id: calltree.c,v 1.8 2002/11/20 23:13:15 moniot Rel $
Routines for producing call trees and cross-reference lists
*/
/*
Copyright (c) 2001 by Robert K. Moniot.
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.
Acknowledgement: the above permission notice is what is known
as the "MIT License."
*/
/*
Shared functions defined:
com_xref_list() Print cross-reference list of com blocks.
visit_children() traverses the call tree, doing some checks
and printing tree if requested.
*/
#include <stdio.h>
#include <string.h>
#include "ftnchek.h"
#include "symtab.h"
#include "pgsymtab.h"
/* Local routines defined. */
PROTO(PRIVATE int block_is_volatile,( ComListHeader *clist, Gsymtab *main_module ));
PROTO(PRIVATE ComListHeader * com_tree_check,( Gsymtab *comblock, Gsymtab
*module, int level ));
PROTO(PRIVATE void visit_child,( Gsymtab *gsymt, int level ));
PROTO(PRIVATE void visit_child_reflist,( Gsymtab *gsymt ));
#ifdef VCG_SUPPORT
PROTO(PRIVATE void visit_child_vcg,( Gsymtab *gsymt, int level ));
#endif
PROTO(PRIVATE ChildList * sort_child_list,( ChildList *child_list ));
PROTO(PRIVATE void print_crossrefs,( void ));
PROTO(PRIVATE void print_cycle_nodes,( Gsymtab gsymt[], int nsym, Gsymtab
*node_list[], int node_count, int
parent_count[] ));
PROTO(PRIVATE int toposort,( Gsymtab gsymt[], int nsym ));
PROTO(PRIVATE ComListHeader * com_declared_by,( Gsymtab *comblock, Gsymtab *module ));
PROTO(PRIVATE void print_modules,( unsigned n, Gsymtab *list[] ));
/* Things used for common undef check */
PRIVATE int com_tree_error;
PRIVATE int numvisited;
/**********************************************************************************
*
* append_extension (imported as-is from ftnchek.c ftnchek 3.1.1)
*
* MODE_DEFAULT_EXT: Adds extension to file name s if
* none is present, and returns a pointer to the
* new name. If extension was added, space is allocated
* for the new name. If not, simply returns pointer
* to original name.
* MODE_REPLACE_EXT: same, except given extension replaces given one if any.
*
* Returns char * to newly allocated name string.
*
**********************************************************************************/
#define MODE_DEFAULT_EXT 1
#define MODE_REPLACE_EXT 2
PRIVATE char *
#if HAVE_STDC
append_extension( char *s, char *ext, int mode )
#else /* K&R style */
append_extension( s, ext, mode )
char *s, *ext;
int mode;
#endif /* HAVE_STDC */
{
int i, len;
char *newname;
#ifdef OPTION_PREFIX_SLASH /* set len=chars to NUL or start
* of /opt */
for ( len = 0; s[len] != '\0' && s[len] != '/'; len++ )
continue;
#else
len = ( unsigned ) strlen( s );
#endif
/*
* Search backwards till find the dot, but do not search past directory
* delimiter
*/
for ( i = len - 1; i > 0; i-- )
{
if ( s[i] == '.'
#ifdef UNIX
|| s[i] == '/'
#endif
#ifdef VMS
|| s[i] == ']' || s[i] == ':'
#endif
#ifdef MSDOS
|| s[i] == '\\' || s[i] == ':'
#endif
)
break;
}
if ( mode == MODE_REPLACE_EXT )
{
if ( s[i] == '.' ) /* declare length = up to the dot */
len = i;
newname =
( char * )
malloc( ( unsigned ) ( len + ( unsigned ) strlen( ext ) + 1 ) );
( void ) strncpy( newname, s, len );
( void ) strcpy( newname + len, ext );
}
else
{ /* MODE_DEFAULT_EXT */
#ifdef OPTION_PREFIX_SLASH
/*
* create new string if new ext or trailing /option
*/
if ( s[i] != '.' || s[len] != '\0' )
{
if ( s[i] != '.' )
{ /* no extension given */
newname = ( char * ) malloc( ( unsigned ) ( len +
( unsigned ) strlen( ext )
+ 1 ) );
( void ) strncpy( newname, s, len );
( void ) strcpy( newname + len, ext );
}
else
{ /* extension given but /option
* follows */
newname = ( char * ) malloc( ( unsigned ) ( len + 1 ) );
( void ) strncpy( newname, s, len );
}
}
#else
if ( s[i] != '.' )
{
newname = ( char * ) malloc( ( unsigned ) ( len +
( unsigned ) strlen( ext ) +
1 ) );
( void ) strcpy( newname, s );
( void ) strcat( newname, ext );
}
#endif
else
{
newname = s; /* use as is */
}
}
return newname;
}
void
com_xref_list(VOID) /* Print cross-reference list of com blocks */
{
#ifdef DYNAMIC_TABLES /* tables will be mallocked at runtime */
Gsymtab **gsymlist,**blocklist;
#else
Gsymtab *gsymlist[GLOBSYMTABSZ],*blocklist[GLOBSYMTABSZ];
#endif
int i,numentries,numblocks;
ComListHeader *cmlist;
#ifdef DYNAMIC_TABLES
if( (gsymlist=(Gsymtab **)calloc(glob_symtab_top,sizeof(Gsymtab *)))
== (Gsymtab **)NULL
||(blocklist=(Gsymtab **)calloc(glob_symtab_top,sizeof(Gsymtab *)))
== (Gsymtab **)NULL
) {
oops_message(OOPS_FATAL,NO_LINE_NUM,NO_COL_NUM,
"Cannot malloc space for common block list");
}
#endif
for(i=numblocks=0;i<glob_symtab_top;i++){ /* loop thru global table */
if (storage_class_of(glob_symtab[i].type) == class_COMMON_BLOCK){
blocklist[numblocks++] = &glob_symtab[i];
}
}
if(numblocks > 0) {
sort_gsymbols(blocklist,numblocks); /* Sort the common block list */
(void)fprintf(list_fd,
"\n Common block cross-reference list:\n");
for(i=0; i<numblocks; i++) {
cmlist = blocklist[i]->info.comlist;
numentries=0;
#ifdef DEBUG_COM_USAGE
(void)fprintf(list_fd, "\n Common Block %s:\n",blocklist[i]->name );
#endif
while (cmlist != NULL){ /* loop thru declarations */
if(! irrelevant(cmlist) &&
(cmlist->any_used || cmlist->any_set))
gsymlist[numentries++] = cmlist->module;
#ifdef DEBUG_COM_USAGE
print_comvar_usage(cmlist);
#endif
cmlist = cmlist->next;
} /* end of while */
if (numentries >0){ /* print modules that declare this block*/
(void)fprintf(list_fd, "\nCommon Block %s used in:\n" ,
blocklist[i]->name );
/* Sort modules that declare this block */
sort_gsymbols(gsymlist,numentries);
print_modules((unsigned)numentries,gsymlist);
} /* end of if numentries >0 */
} /* end of for i = 0 to numblocks */
(void)fprintf(list_fd,"\n");
} /* end of if numblocks > 0*/
}
void
visit_children(VOID)
{
int i,
num_mains, /* number of main programs */
num_roots; /* number of uncalled nonlibrary modules */
Gsymtab* main_module;
num_roots = 0;
for(i=0; i<glob_symtab_top; i++) {
if(storage_class_of(glob_symtab[i].type) == class_SUBPROGRAM
&& ! glob_symtab[i].internal_entry) {
glob_symtab[i].link.child_list=
sort_child_list(glob_symtab[i].link.child_list);
/* Count defined but uncalled non-library modules for use later */
if(glob_symtab[i].defined && !glob_symtab[i].used_flag &&
!glob_symtab[i].library_module)
++num_roots; /* Count tree roots for use if no mains */
}
}
if(print_ref_list)
(void)fprintf(list_fd,"\nList of subprogram references:");
#ifdef VCG_SUPPORT
else if(print_vcg_list) {
if(vcg_fd == stdout)
(void)fprintf(vcg_fd,"\n");
(void)fprintf(vcg_fd,"graph: {\ntitle: \"%s\"\n",main_filename);
/* Global graph options go here. See ftnchek.h.
*/
(void)fprintf(vcg_fd,VCG_GRAPH_OPTIONS);
}
#endif
else if(print_call_tree)
(void)fprintf(list_fd,"\nTree of subprogram calls:");
/* Visit children of all main progs */
for(i=0,num_mains=0; i<glob_symtab_top; i++) {
if(glob_symtab[i].type == type_byte(class_SUBPROGRAM,type_PROGRAM)) {
main_module = &glob_symtab[i];
if(print_ref_list)
visit_child_reflist(main_module);
#ifdef VCG_SUPPORT
else if(print_vcg_list)
visit_child_vcg(main_module,1);
#endif
else
visit_child(main_module,0);
++num_mains;
}
}
/* If no main program found, give
warning unless -noextern was set */
if(num_mains == 0) {
if(print_call_tree || print_ref_list
#ifdef VCG_SUPPORT
|| print_vcg_list
#endif
) {
(void)fprintf(list_fd,"\n (no main program found)");
}
else if(usage_ext_undefined) {
(void)fprintf(list_fd,
"\nNo main program found");
}
/* If no main, visit trees rooted at uncalled
nonlibrary routines, as the next best thing.
If there are no uncalled nonlib modules, use
uncalled library routines. If there are no uncalled
routines, then there is a cycle!
*/
for(i=0; i<glob_symtab_top; i++) {
if(storage_class_of(glob_symtab[i].type) == class_SUBPROGRAM
&& glob_symtab[i].defined && !glob_symtab[i].used_flag &&
(num_roots == 0 || !glob_symtab[i].library_module) ) {
if(print_ref_list)
visit_child_reflist(&glob_symtab[i]);
#ifdef VCG_SUPPORT
else if(print_vcg_list)
visit_child_vcg(&glob_symtab[i],1);
#endif
else
visit_child(&glob_symtab[i],1); /* indent all trees one level */
}
}
}
if(print_call_tree || print_ref_list)
(void)fprintf(list_fd,"\n");
#ifdef VCG_SUPPORT
if(print_vcg_list)
(void)fprintf(vcg_fd,"}\n");
#endif
/* Print list of callers of all visited
or non-library modules, if -crossref
flag given. */
if(print_xref_list) {
print_crossrefs();
}
/* Print linkage-order list of modules. */
if( print_topo_sort ) {
(void) toposort(glob_symtab,(int)glob_symtab_top);
}
/* Check that common blocks retain definition
status between uses. */
if(check_com_tree || comcheck_volatile){
if(num_mains != 1) {
if(check_com_tree)
(void)fprintf(list_fd,
"\nCommon definition check requires single main program");
if(comcheck_volatile)
(void)fprintf(list_fd,
"\nCommon volatility check requires single main program");
}
else {
numvisited = 0; /* need headcount in case of cycle */
for(i=0; i<glob_symtab_top; i++) {
if(glob_symtab[i].visited_somewhere)
numvisited++;
}
for(i=0; i<glob_symtab_top; i++) {
if(storage_class_of(glob_symtab[i].type) == class_COMMON_BLOCK) {
if( block_is_volatile(glob_symtab[i].info.comlist,main_module) ) {
if(comcheck_volatile) {
(void)fprintf(list_fd,
"\nCommon block %s is volatile",
glob_symtab[i].name);
}
if(check_com_tree) {
com_tree_error=0;
(void)com_tree_check(&glob_symtab[i],main_module,0);
}
}
}
}
}
}
}
/* Returns TRUE unless block is SAVED by any module, or declared by
the actual main program or in a BLOCK DATA subprogram. */
PRIVATE int
#if HAVE_STDC
block_is_volatile(ComListHeader *clist, Gsymtab *main_module)
#else /* K&R style */
block_is_volatile(clist,main_module)
ComListHeader *clist;
Gsymtab *main_module;
#endif /* HAVE_STDC */
{
int t;
while(clist != NULL) {
if( clist->saved ||
(t=datatype_of(clist->module->type)) == type_BLOCK_DATA
|| (t == type_PROGRAM && clist->module == main_module)) {
return FALSE;
}
clist = clist->next;
}
return TRUE;
}
/* If block declared by module, returns pointer to the comlist
header which describes it. Otherwise returns NULL. */
PRIVATE ComListHeader *
#if HAVE_STDC
com_declared_by(Gsymtab *comblock, Gsymtab *module)
#else /* K&R style */
com_declared_by(comblock,module)
Gsymtab *comblock,*module;
#endif /* HAVE_STDC */
{
ComListHeader *clist=comblock->info.comlist;
while(clist != NULL) {
if(clist->module == module) {
if(clist->saved) {
com_tree_error = TRUE; /* not so, but causes bailout */
}
return clist;
}
clist = clist->next;
}
return NULL;
}
/* Checks whether common block can become undefined
between activations of some module that declares it.
Should only be done for blocks that are volatile, i.e.
that are not SAVED or declared in main or block_data.
Rules used are:
(1) Block is declared in two subtrees whose roots
are called by a given module, and not in
the given module itself or above.
(2) Block is declared and elements accessed in a module
called by a given module, and not declared in the
module itself or above. (Module that declares it but
does not access elements, can be holding the
block active for its children.)
Since Rule 2 is likely to be wrong often due to Ftnchek's
lack of knowledge about whether a routine is invoked
more than once, it is suppressed for now.
*/
PRIVATE ComListHeader *
#if HAVE_STDC
com_tree_check(Gsymtab *comblock, Gsymtab *module, int level)
#else /* K&R style */
com_tree_check(comblock,module,level)
Gsymtab *comblock,*module;
int level;
#endif /* HAVE_STDC */
{
ComListHeader *clist;
/* The following only protects against recursion. It is not
a full-fledged cycle detector just a stopper. */
if(level > numvisited) {
(void)fprintf(list_fd,
"\nWarning: Call tree has a cycle containing module %s\n",
module->name);
com_tree_error = TRUE;
return NULL;
}
/* If this module declares the block, return its clist */
if( (clist=com_declared_by(comblock,module)) != NULL) {
#ifdef DEBUG_SAVE
(void)fprintf(list_fd,"\n%s declared by %s",comblock->name,module->name);
#endif
return clist;
}
else { /* Otherwise see if it is declared in subtree */
int any_child_declares_it;
ComListHeader *declaring_clist, *this_clist;
ChildList *child_list;
any_child_declares_it=FALSE;
declaring_clist=NULL;
/* Scan list of children */
child_list = (module->internal_entry?module->link.module:module)
->link.child_list;
while(child_list != NULL) {
this_clist = com_tree_check(comblock,child_list->child,level+1);
/* Error was detected below: bail out */
if(com_tree_error) {
return NULL;
}
else if(this_clist != NULL) {
/* Subtree contains the block */
if(any_child_declares_it /* Rule 1 */
#ifdef COMTREE_RULE_2
|| (this_clist->any_used || this_clist->any_set) /* Rule 2 */
#endif
){
cmp_error_count = 0;
(void)comcmp_error_head(comblock->name,this_clist,
"may become undefined between activations");
com_error_report(this_clist,"Declared");
if(declaring_clist != NULL && declaring_clist != this_clist) {
com_error_report(declaring_clist,"Declared");
}
(void)fprintf(list_fd,"\n ");
(void)fprintf(list_fd,
"Not declared in parent module %s",
module->name);
com_tree_error = TRUE;
return NULL;
}
else {
any_child_declares_it = TRUE;
declaring_clist = this_clist;
}
}
child_list = child_list->next;
}
/* If any subtree declares it, say so */
return declaring_clist;
}
}
/* Depth-first search of call tree */
PRIVATE void
#if HAVE_STDC
visit_child(Gsymtab *gsymt, int level)
#else /* K&R style */
visit_child(gsymt,level)
Gsymtab *gsymt;
int level;
#endif /* HAVE_STDC */
{
static char fmt[]="%000s"; /* Variable format for indenting names */
ChildList *child_list;
static int terminate_href = 0;
char *fname=NULL;
ArgListHeader *arghdr;
if(print_call_tree) {
if ( htmlcalltree_fd ) {
/* Look up defn arglist entry to find the
filename where this guy is defined.
*/
for ( arghdr = gsymt->info.arglist; arghdr; arghdr=arghdr->next )
{
if ( arghdr->is_defn && arghdr->filename )
{
fname = append_extension( arghdr->filename,
DEF_HTML_EXTENSION, MODE_REPLACE_EXT );
}
}
if ( terminate_href )
{
terminate_href = 0;
( void ) fprintf( htmlcalltree_fd, "</A>" );
}
( void ) fprintf( htmlcalltree_fd, "\n" );
}
(void)fprintf(list_fd,"\n");
if(level > 0) {
(void)sprintf(fmt,"%%%ds",level*4); /* indent 4 spaces per nesting level */
(void)fprintf(list_fd,fmt,"");
if ( htmlcalltree_fd )
{
if ( fname )
{
if ( ! gsymt->internal_entry )
( void ) fprintf( htmlcalltree_fd, "%*.*s<A href=\"%s#%s\">",
level * 4, level * 4, " ",
fname, gsymt->name );
else
( void ) fprintf( htmlcalltree_fd, "%*.*s<A href=\"%s#%s\">",
level * 4, level * 4, " ",
fname, gsymt->link.module->name );
terminate_href = 1;
}
else
{
( void ) fprintf( htmlcalltree_fd, "%*.*s", level * 4, level * 4,
" " );
terminate_href = 0;
}
}
}
if(gsymt->internal_entry)
{
(void)fprintf(list_fd,"%s entry ",gsymt->link.module->name);
if ( htmlcalltree_fd )
( void ) fprintf( htmlcalltree_fd, "%s entry ",
gsymt->link.module->name );
}
(void)fprintf(list_fd,"%s",gsymt->name);
if ( htmlcalltree_fd )
{
if ( level == 0 )
{
if ( gsymt->internal_entry )
{
if(fname)
( void ) fprintf( htmlcalltree_fd, "<A href=\"%s#%s\">%s",
fname, gsymt->link.module->name, gsymt->name );
}
else
{
( void ) fprintf( htmlcalltree_fd, "<A href=\"%s#%s\">%s",
fname, gsymt->name, gsymt->name );
}
terminate_href = 1;
}
else
( void ) fprintf( htmlcalltree_fd, "%s", gsymt->name );
}
if(fname)
free( fname );
}
/* Visit its unvisited children. Note
that children of internal entry are
taken as those of its superior module.
*/
child_list = (gsymt->internal_entry?gsymt->link.module:gsymt)
->link.child_list;
/* If already visited, do not visit its
children, but give note to reader if it
has some. */
if(call_tree_prune && gsymt->visited) {
if(print_call_tree && child_list != NULL)
{
(void)fprintf(list_fd," (see above)");
if ( htmlcalltree_fd )
{
if ( terminate_href )
{
( void )fprintf( htmlcalltree_fd, "</A>" );
terminate_href=0;
}
( void ) fprintf( htmlcalltree_fd, " (see above)" );
}
}
}
else {
/* Mark node as visited */
gsymt->visited = TRUE;
/* Record that containing module
is visited via this entry point*/
if(gsymt->internal_entry)
gsymt->link.module->visited_somewhere = TRUE;
else
gsymt->visited_somewhere = TRUE;
if ( print_call_tree )
{
if ( terminate_href )
{
( void ) fprintf( htmlcalltree_fd, "</A>" );
terminate_href = 0;
}
}
++level; /* move to next level */
while(child_list != NULL) {
visit_child(child_list->child,level);
child_list = child_list->next;
}
}
if ( terminate_href )
{
( void ) fprintf( htmlcalltree_fd, "</A>" );
terminate_href = 0;
}
}
/*** visit_child_reflist
Same as visit_child, except it does a breadth-first search of the call
tree, and prints the results in the form of a who-calls-who list.
Contributed by: Gerome Emmanuel : Esial Troisieme annee
Projet commun Esial / Ecole des mines
INERIS
E-mail: gerome@mines.u-nancy.fr
Date received: 20-APR-1993
Modified slightly to make it compatible as alternative to call-tree and
to make output format consistent.
***/
PRIVATE void
#if HAVE_STDC
visit_child_reflist(Gsymtab *gsymt)
#else /* K&R style */
visit_child_reflist(gsymt)
Gsymtab *gsymt;
#endif /* HAVE_STDC */
{
ChildList *child_list;
child_list = (gsymt->internal_entry?gsymt->link.module:gsymt)
->link.child_list;
/* If already visited, do not visit its
children, but give note to reader if it
has some. */
if(!gsymt->visited) {
/* Mark node as visited */
gsymt->visited = TRUE;
/* Record that containing module
is visited via this entry point*/
if(gsymt->internal_entry)
gsymt->link.module->visited_somewhere = TRUE;
else
gsymt->visited_somewhere = TRUE;
if(print_ref_list) /* Print callees neatly if desired */
{
#ifdef DYNAMIC_TABLES /* tables will be mallocked at runtime */
Gsymtab **gsymlist;
#else
Gsymtab *gsymlist[GLOBSYMTABSZ];
#endif
ChildList *child_list2;
unsigned numcalls;
#ifdef DYNAMIC_TABLES
if( (gsymlist=(Gsymtab **)calloc(glob_symtab_top,sizeof(Gsymtab *)))
== (Gsymtab **)NULL) {
oops_message(OOPS_FATAL,NO_LINE_NUM,NO_COL_NUM,
"Cannot malloc space for reference list");
}
#endif
(void)fprintf(list_fd,"\n%s calls:",gsymt->name);
numcalls = 0;
child_list2 = child_list;
while(child_list2 != NULL)
{
gsymlist[numcalls++] = child_list2->child;
child_list2 = child_list2->next;
}
if(numcalls == (unsigned)0)
(void)fprintf(list_fd," none");
else {
(void)fprintf(list_fd,"\n");
print_modules(numcalls,gsymlist);
}
#ifdef DYNAMIC_TABLES
(void) cfree(gsymlist);
#endif
}
while(child_list != NULL) {
visit_child_reflist(child_list->child);
child_list = child_list->next;
}
}
}
/* visit_child_vcg:
Same as visit_child_reflist except it provides output suitable for
visualisation of the call graph, using the vcg graph visualisation
program. VCG is freely available from ftp.cs.uni-sb.de and
elsewhere. It was written by G. Sander of the University of
Saarland, Germany.
Contributed by: P.A.Rubini@cranfield.ac.uk
Date: 3-APR-1995
*/
#ifdef VCG_SUPPORT
PRIVATE void
#if HAVE_STDC
visit_child_vcg(Gsymtab *gsymt, int level)
#else /* K&R style */
visit_child_vcg(gsymt,level)
Gsymtab *gsymt;
int level;
#endif /* HAVE_STDC */
{
ArgListHeader *arglist;
ChildList *child_list;
child_list = (gsymt->internal_entry?gsymt->link.module:gsymt)
->link.child_list;
/* If already visited, do not visit its
children, but give note to reader if it
has some. */
if(!gsymt->visited) {
/* Mark node as visited */
gsymt->visited = TRUE;
/* Record that containing module
is visited via this entry point*/
if(gsymt->internal_entry)
gsymt->link.module->visited_somewhere = TRUE;
else
gsymt->visited_somewhere = TRUE;
if(print_vcg_list) /* Print callees neatly if desired */
{
#ifdef DYNAMIC_TABLES /* tables will be mallocked at runtime */
Gsymtab **gsymlist;
#else
Gsymtab *gsymlist[GLOBSYMTABSZ];
#endif
ChildList *child_list2;
int j;
int numcalls;
#ifdef DYNAMIC_TABLES
if( (gsymlist=(Gsymtab **)calloc(glob_symtab_top,sizeof(Gsymtab *)))
== (Gsymtab **)NULL) {
oops_message(OOPS_FATAL,NO_LINE_NUM,NO_COL_NUM,
"Cannot malloc space for reference list");
}
#endif
numcalls = 0;
child_list2 = child_list;
while(child_list2 != NULL)
{
gsymlist[numcalls++] = child_list2->child;
child_list2 = child_list2->next;
}
arglist = gsymt->info.arglist;
while(arglist != NULL) {
if ( arglist->is_defn ) {
(void)fprintf(vcg_fd,"\ngraph: {\ntitle:\"[%s]\"\n",gsymt->name);
(void)fprintf(vcg_fd,
"node: { title: \"%s\" label: \"%s \\n (%s)\" info1:\"%d\" }\n",
gsymt->name,gsymt->name,
arglist->filename,
level );
if(numcalls != 0) {
for (j=0;j<numcalls;j++){
arglist = gsymlist[j]->info.arglist;
while(arglist != NULL) {
if ( arglist->is_defn ) {
(void)fprintf(vcg_fd,
"edge: { sourcename: \"%s\" targetname: \"%s\" class:%d} \n",
gsymt->name,gsymlist[j]->name,
level );
break ;
}
arglist = arglist->next;
}
}
}
break;
}
arglist = arglist->next;
}
#ifdef DYNAMIC_TABLES
(void) cfree(gsymlist);
#endif
++level; /* move to next level */
/* while(child_list != NULL) {
visit_child_vcg(child_list->child,level);
child_list = child_list->next;
} */
for (j=0;j<numcalls;j++){
arglist = gsymlist[j]->info.arglist;
while(arglist != NULL) {
if ( arglist->is_defn ) {
visit_child_vcg(gsymlist[j],level);
break ;
}
arglist = arglist->next;
}
}
(void)fprintf(vcg_fd,"}\n");
}
}
}
#endif /* VCG_SUPPORT */
PRIVATE void
print_crossrefs(VOID)
{
#ifdef DYNAMIC_TABLES /* tables will be mallocked at runtime */
Gsymtab **gsymlist, **modulelist;
#else
Gsymtab *gsymlist[GLOBSYMTABSZ], *modulelist[GLOBSYMTABSZ];
#endif
ArgListHeader *args;
int i,numentries;
int numcalls;
#ifdef DYNAMIC_TABLES
if( (gsymlist=(Gsymtab **)calloc(glob_symtab_top,sizeof(Gsymtab *)))
== (Gsymtab **)NULL ||
(modulelist=(Gsymtab **)calloc(glob_symtab_top,sizeof(Gsymtab *)))
== (Gsymtab **)NULL) {
oops_message(OOPS_FATAL,NO_LINE_NUM,NO_COL_NUM,
"Cannot malloc space for crossref list");
}
#endif
/* Gather up all relevant subprograms */
for(i=0,numentries=0; i<glob_symtab_top; i++) {
if(storage_class_of(glob_symtab[i].type) == class_SUBPROGRAM
&& (glob_symtab[i].visited || !glob_symtab[i].library_module)) {
gsymlist[numentries++] = &glob_symtab[i];
}
}
if(numentries > 0) {
(void)fprintf(list_fd,"\n\n Cross-reference list:\n");
/* Sort the subprograms */
sort_gsymbols(gsymlist,numentries);
/* Print their callers */
for(i=0; i<numentries; i++) {
(void)fprintf(list_fd,"\n");
if(gsymlist[i]->internal_entry)
(void)fprintf(list_fd,"%s entry ",gsymlist[i]->link.module->name);
(void)fprintf(list_fd,"%s",gsymlist[i]->name);
numcalls=0;
args = gsymlist[i]->info.arglist;
while(args != NULL) { /* Gather up callers */
if(!args->is_defn) {
/* (eliminate duplicates) */
if(numcalls==0 || args->module != modulelist[numcalls-1])
modulelist[numcalls++] = args->module;
}
args = args->next;
}
if(numcalls == 0) {
(void)fprintf(list_fd," not called");
if(datatype_of(gsymlist[i]->type) == type_PROGRAM)
(void)fprintf(list_fd," (main program)");
}
else {
(void)fprintf(list_fd," called by:\n");
sort_gsymbols(modulelist,numcalls); /* Sort the callers */
print_modules(numcalls,modulelist);
}
}
(void)fprintf(list_fd,"\n");
}
#ifdef DYNAMIC_TABLES
(void) cfree(gsymlist);
(void) cfree(modulelist);
#endif
}
/* Topological sort of the call tree. Based closely on algorithm
on page 314 of Horowitz and Sahni, Fundamentals of Data
Structures. Returns TRUE if successful, FALSE if failed
due to a cycle being detected.
*/
PRIVATE int
#if HAVE_STDC
toposort(Gsymtab *gsymt, int nsym)
#else /* K&R style */
toposort(gsymt,nsym)
Gsymtab gsymt[];
int nsym;
#endif /* HAVE_STDC */
{
int i,num_nodes, node_count;
ChildList *child_list;
Gsymtab *child_module; /* Called module's top entry point */
#ifdef DYNAMIC_TABLES /* tables will be mallocked at runtime */
int *parent_count;
Gsymtab **node_list;
#else
int parent_count[GLOBSYMTABSZ];
Gsymtab *node_list[GLOBSYMTABSZ];
#endif
#ifdef DYNAMIC_TABLES
if( (parent_count=(int *)calloc(glob_symtab_top,sizeof(int)))
== (int *)NULL ||
(node_list=(Gsymtab **)calloc(glob_symtab_top,sizeof(Gsymtab *)))
== (Gsymtab **)NULL) {
oops_message(OOPS_FATAL,NO_LINE_NUM,NO_COL_NUM,
"Cannot malloc space for module sort");
}
#endif
/* Initialize array of links/counts */
for(i=0; i<nsym; i++)
parent_count[i] = 0; /* In-order of module as node */
/* Traverse child lists, incrementing their
parent counts.
*/
for(i=0,num_nodes=0; i<nsym; i++) {
if(gsymt[i].visited_somewhere) { /* skip entry pts and com blocks */
++num_nodes;
child_list = gsymt[i].link.child_list;
while(child_list != NULL) {
/* If child is an internal entry, substitute
top entry point of its subprogram unit. */
if( (child_module=child_list->child)->internal_entry )
child_module = child_module->link.module;
++parent_count[child_module - gsymt]; /* index into table */
child_list = child_list->next;
}
}
}
{ /* Start of the sort */
int top=0;
int j,k;
for(i=0; i<nsym; i++) {
if(gsymt[i].visited_somewhere && parent_count[i] == 0) {
parent_count[i] = top; /* Link now-parentless module into stack */
top = i+1;
}
}
for(i=0,node_count=0; i<num_nodes; i++) {
if(top == 0) {
if(print_topo_sort) {
(void)fprintf(list_fd,"\nCall tree has a cycle");
print_cycle_nodes(gsymt,nsym,node_list,node_count,parent_count);
}
break;
}
j = top-1;
top = parent_count[j]; /* Recover the link */
/* Print the next module */
if(print_topo_sort) {
node_list[node_count++] = &gsymt[j];
parent_count[j] = -1;
}
/* Decrease parent count of its children */
child_list = gsymt[j].link.child_list;
while(child_list != NULL) {
if( (child_module=child_list->child)->internal_entry )
child_module = child_module->link.module;
k = child_module - gsymt;
if(--parent_count[k] == 0) { /* Now parentless? Stack it*/
parent_count[k] = top;
top = k+1;
}
child_list = child_list->next;
}
}
}/*end sort*/
if(print_topo_sort && node_count > 0) {
(void)fprintf(list_fd,"\nList of called modules in prerequisite order:\n");
print_modules(node_count,node_list);
(void)fprintf(list_fd,"\n");
}
#ifdef DYNAMIC_TABLES
(void) cfree(parent_count);
(void) cfree(node_list);
#endif
return (node_count==num_nodes); /* Success = TRUE */
}
/* Traces back to find nodes not listed in topological
sort. They are the cycle nodes and their descendants.
*/
PRIVATE void
#if HAVE_STDC
print_cycle_nodes(Gsymtab *gsymt, int nsym, Gsymtab **node_list, int node_count, int *parent_count)
#else /* K&R style */
print_cycle_nodes(gsymt,nsym,node_list,node_count,parent_count)
Gsymtab gsymt[];
int nsym;
Gsymtab *node_list[];
int node_count;
int parent_count[];
#endif /* HAVE_STDC */
{
int i;
int k=node_count;
for(i=0; i<nsym; i++) {
if(gsymt[i].visited_somewhere) {
if(parent_count[i] != -1) /* Not tagged */
node_list[k++] = &gsymt[i];
}
}
if(k > node_count)
(void)fprintf(list_fd," containing some of the following modules:\n");
print_modules(k-node_count,node_list+node_count);
}
/* Insertion sort of child list.
Also removes duplicates which
can be introduced via multiple
defns or via project files. */
PRIVATE ChildList *
#if HAVE_STDC
sort_child_list(ChildList *child_list)
#else /* K&R style */
sort_child_list(child_list)
ChildList *child_list;
#endif /* HAVE_STDC */
{
if( call_tree_sort ) {
ChildList *front,*prev,*next,*cl=child_list;
Gsymtab *temp;
prev = NULL;
while(cl != NULL) {
/* Scan thru list for lexicographically lowest name */
front=cl;
for(next=cl->next; next != NULL; next = next->next) {
if(strcmp(front->child->name,next->child->name) > 0) {
front = next;
}
}
/* Swap child pointers so front is first */
if(front != cl) {
temp = front->child;
front->child = cl->child;
cl->child = temp;
}
/* If duplicate, remove from list */
if(prev != NULL && prev->child == cl->child)
prev->next = cl->next;
else
prev = cl;
cl = cl->next;
}
return child_list;
}
else /* put children in program order, i.e. reverse the list */
{
ChildList *curr,*next,*temp;
if(child_list == NULL)
return child_list;
curr = child_list;
next = curr->next;
while(next != NULL) {
temp = next->next;
next->next = curr; /* switch the pointers to point in reverse */
curr = next;
next = temp;
}
child_list->next = NULL; /* former head is now tail */
return curr; /* and curr now points to new head */
}
}
PRIVATE void
#if HAVE_STDC
print_modules(unsigned int n, Gsymtab **list) /* formatting of module names */
#else /* K&R style */
print_modules(n,list) /* formatting of module names */
unsigned n;
Gsymtab *list[];
#endif /* HAVE_STDC */
{
COLNO_t col=0;
unsigned len,j;
for (j=0;j<n;j++){
if(list[j]->internal_entry) {
len=strlen(list[j]->link.module->name);
col+= len= (len<=10? 10:len) +9;
if (col >78){
fprintf(list_fd, "\n");
col = len;
} /* end of if */
fprintf(list_fd," %10s entry",list[j]->link.module->name);
len=strlen(list[j]->name)+1;
col+= len;
if (col >78){
fprintf(list_fd, "\n");
col = len;
} /* end of if */
fprintf(list_fd," %s",list[j]->name);
}
else {
len=strlen(list[j]->name);
col+= len= (len<=10? 10:len) +3;
if (col >78){
(void)fprintf(list_fd, "\n");
col = len;
} /* end of if */
(void)fprintf(list_fd," %10s",list[j]->name);
}
} /* end of for */
}
/** End of common block and variable usage checks **/
|