/* $Id: prlocsym.c,v 1.22 2003/03/17 17:47:56 moniot Exp $ Prints local symbol table, calling local checking routines along the way. */ /* 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: print_loc_symbols(curmodhash) Prints local symtab info. */ #include #include #include #include "ftnchek.h" #include "symtab.h" #include "plsymtab.h" #include "loccheck.h" PROTO(PRIVATE void print_io_unit_usages,(VOID)); PROTO(PRIVATE void sort_io_unit_usages,(VOID)); PROTO(PRIVATE int cmp_io_units, ( IO_Unit_Info *u1, IO_Unit_Info *u2 )); void print_loc_symbols(VOID) { #ifdef DYNAMIC_TABLES /* tables will be mallocked at runtime */ static Lsymtab **sym_list=(Lsymtab **)NULL; #else Lsymtab *sym_list[LOCSYMTABSZ]; /* temp. list of symtab entries to print */ #endif int mod_type, /* datatype of this module */ this_is_a_function; /* flag for treating funcs specially */ Lsymtab *module; /* entry of current module in symtab */ char *mod_name; /* module name */ int imps=0, /* count of implicitly declared identifiers */ numentries; /* count of entry points of module */ if (dcl_fd == (FILE*)NULL) dcl_fd = stdout; #ifdef DYNAMIC_TABLES if(sym_list == (Lsymtab **)NULL) { /* Initialize if not done before */ if( (sym_list=(Lsymtab **)calloc(LOCSYMTABSZ,sizeof(Lsymtab *))) == (Lsymtab **)NULL) { oops_message(OOPS_FATAL,NO_LINE_NUM,NO_COL_NUM, "Cannot malloc space for local symbol list"); } } #endif /* Keep track of statement counts for -resource */ tot_exec_stmt_count += exec_stmt_count; if(exec_stmt_count > max_exec_stmt_count) max_exec_stmt_count = exec_stmt_count; /* Keep track of symbol table and string usage */ if(loc_symtab_top > max_loc_symtab) { max_loc_symtab = loc_symtab_top; } if(loc_str_top + extra_locstrspace > max_loc_strings) { max_loc_strings = loc_str_top + extra_locstrspace; } if(srctextspace_top + extra_srctextspace > max_srctextspace) { max_srctextspace = srctextspace_top + extra_srctextspace; } if(token_head_space_top + extra_tokheadspace > max_tokenlists) { max_tokenlists=token_head_space_top + extra_tokheadspace; } if(param_info_space_top + extra_paraminfospace > max_paraminfo) { max_paraminfo=param_info_space_top + extra_paraminfospace; } if(token_space_top + extra_tokspace > max_token_space) { max_token_space = token_space_top + extra_tokspace; } if(ptrspace_top + extra_ptrspace > max_ptrspace) { max_ptrspace = ptrspace_top + extra_ptrspace; } update_label_resources(); /* Do the same in label handler */ /* Global symbols only increase in number */ max_glob_symtab = glob_symtab_top; /* Set up name & type, and see what kind of module it is */ module = hashtab[current_module_hash].loc_symtab; mod_name = module->name; mod_type = get_type(module); if( mod_type != type_PROGRAM && mod_type != type_SUBROUTINE && mod_type != type_COMMON_BLOCK && mod_type != type_BLOCK_DATA ) this_is_a_function = TRUE; else this_is_a_function = FALSE; /* Print name & type of the module */ if(do_symtab) { int i; for(i=0,numentries=0;i 1) { sort_lsymbols(sym_list,numentries); } (void)fprintf(list_fd,"\n\nModule %s:",mod_name); if( this_is_a_function ) (void)fprintf(list_fd," func:"); (void)fprintf(list_fd," %4s",type_name[mod_type]); /* Print a * next to non-declared function name */ if(datatype_of(module->type) == type_UNDECL ) { (void)fprintf(list_fd,"*"); imps++; } (void)fprintf(list_fd,"\n"); /* Print Entry Points (skip if only one, since it is same as module name) */ if(do_symtab && numentries > 1) { (void)fprintf(list_fd,"\nEntry Points\n"); (void) print_lsyms_briefly(sym_list,numentries,FALSE); } /* End of printing module name and entry points */ }/*if(do_symtab)*/ /* Print the externals */ if(do_symtab) { int i,n; for(i=0,n=0;iintrins_flags & (f77_intrinsics?I_NONF77:I_NONF90))) { sym_list[n++] = &loc_symtab[i]; } } if(n != 0) { sort_lsymbols(sym_list,n); local_warn_head(mod_name, choose_filename(sym_list[0],file_used), sym_list[0]->line_used, (Lsymtab *)NULL, FALSE, f77_intrinsics? "Non Fortran 77" : "Non Fortran 90"); msg_tail("intrinsic functions referenced:"); (void) print_lsyms(sym_list,n,FALSE); } }/*if(f77_intrinsics || f90_intrinsics)*/ /* issue -f77 warning for identifiers longer than 6 characters */ if(f77_long_names) { int i,n; for(i=0,n=0;i (unsigned)6) sym_list[n++] = &loc_symtab[i]; } if(n != 0) { sort_lsymbols(sym_list,n); local_warn_head(mod_name, top_filename, NO_LINE_NUM, sym_list[0], FALSE, "Names longer than 6 chars (nonstandard):"); (void) print_lsyms(sym_list,n,FALSE); } } /* If -f77 flag given, list names with underscore or dollarsign */ if(f77_underscores || f77_dollarsigns || f90_dollarsigns) { int n; n = find_nonalnum_names(sym_list); if(n != 0) { sort_lsymbols(sym_list,n); local_warn_head(mod_name, top_filename, NO_LINE_NUM, sym_list[0], FALSE, "Names containing nonstandard characters:"); (void) print_lsyms(sym_list,n,FALSE); } }/*if(f77_underscores || f77_dollarsigns || f90_dollarsigns)*/ /* Print out clashes in first six chars of name */ if(sixclash) { int n; n = find_sixclashes(sym_list); if(n != 0) { sort_lsymbols(sym_list,n); /* Use the right line number */ local_warn_head(mod_name, top_filename, NO_LINE_NUM, sym_list[0], FALSE, "Identifiers which are not unique in first six chars:"); (void) print_lsyms(sym_list,n,FALSE); }/* end if(n != 0) */ }/* end if(sixclash) */ /* If portability flag was given, check equivalence groups for mixed type. */ if(port_mixed_equiv || port_mixed_size || local_wordsize==0) { int i,j,n; int port_imps=0; Lsymtab *equiv; /* scan thru table for equivalenced variables */ for(i=0;iequiv_link; } while(equiv != &loc_symtab[i]); /* complete the circle */ /* Check for mixed types */ if(n != 0) { int mixed_type = FALSE, mixed_size = FALSE, mixed_default_size = FALSE; int t1,t2,s1,s2,defsize1,defsize2; t1 = get_type(sym_list[0]); s1 = get_size(sym_list[0],t1); defsize1 = (s1 == size_DEFAULT); if(s1 == size_DEFAULT) s1 = type_size[t1]; for(j=1; j 0 ) { print_io_unit_usages(); } /* print table of stmt labels */ if(misc_warn || usage_label_undefined || usage_label_unused || do_symtab || print_lab_refs) { sort_labtable(); if(do_symtab) { print_labels(); } if(print_lab_refs) { print_label_refs(); } if(misc_warn || usage_label_undefined || usage_label_unused) { check_labels(mod_name); } } make_declarations(sym_list,mod_name); /* Recreate a FULL list of local symbols */ { int i, n; for(i=0,n=0;i= 0 && no >= 0)? "=": " "); if( no < 0 ) fprintf(list_fd,"%7s",""); else fprintf(list_fd,"%-7d",no); fprintf(list_fd," %4s %4s %-9s", IO_access[io_unit_info[i].io_access], IO_form[io_unit_info[i].io_form], keytok_name(op)); old_id = id; old_no = no; old_op = op; old_acc = io_unit_info[i].io_access; old_form = io_unit_info[i].io_form; cols = 43; /* amount of stuff printed so far */ } /* print line number(s) after info */ if(cols+7 > wrap_column) { fprintf(list_fd,"\n%43s",""); cols = 43; } fprintf(list_fd,"%6d ", io_unit_info[i].line_num); cols = cols+7; } } /* Routine to sort the io_unit_usages array. It uses bubble sort * since the array is normally not very long and should be nearly * sorted already in most cases. */ PRIVATE void sort_io_unit_usages(VOID) { int i, j; for(i = 0; i < num_io_unit_usages-1; i++) { int numswaps=0; for(j = num_io_unit_usages-2; j >= i; j--) { if( cmp_io_units(&io_unit_info[j],&io_unit_info[j+1]) > 0 ) { IO_Unit_Info hold = io_unit_info[j+1]; io_unit_info[j+1] = io_unit_info[j]; io_unit_info[j] = hold; numswaps++; } } if(numswaps == 0) break; } } /* Function to determine ordering of two I/O unit info entries. The * primary sort key is the unit number or unit id. If unit number is * known, it takes precedence, and sort is in numeric order. * Otherwise sort is alphabetic by unit id, with the default unit=* * treated as an id that comes before all alphabetic names. If unit * number or id compares equal, then sort is by operation, access and * form. (It should be rare for access and form to differ if * operation is the same, but the possibility cannot be ruled out.) * Line number is not used as a sort key since list arrives in * line-number order and bubble sort preserves initial order. */ PRIVATE int cmp_io_units( IO_Unit_Info *u1, IO_Unit_Info *u2 ) { int no1 = u1->unit_no; int no2 = u2->unit_no; if( no1 == no2 ) { /* same unit no or both unknown */ /* if unit no unknown, compare unit id name */ if( no1 == IO_UNIT_UNKNOWN ) { int id1 = u1->unit_id; int id2 = u2->unit_id; if( id1 == id2 ) { /* same name: sort by operation, form, access */ if( u1->io_operation != u2->io_operation ) return u1->io_operation - u2->io_operation; else if( u1->io_access != u2->io_access ) return u1->io_access - u2->io_access; else return u1->io_form - u2->io_form; } else { /* different names: sort by name */ if( id1 == IO_UNIT_DEFAULT ) { return -1; /* '*' precedes all names */ } else if( id2 == IO_UNIT_DEFAULT ) { return 1; } else if( id1 >= 0 && id2 >= 0 ) { /* they have names: compare */ return strcmp(hashtab[id1].name,hashtab[id2].name); } else { return id1 - id2; /* shouldn't happen */ } } } else { /* if same unit no, sort by operation */ if( u1->io_operation != u2->io_operation ) return u1->io_operation - u2->io_operation; else if( u1->io_access != u2->io_access ) return u1->io_access - u2->io_access; else return u1->io_form - u2->io_form; } } else { /* diff unit nos */ if( no1 == IO_UNIT_UNKNOWN ) { return 1; /* unit ids follow unit nos */ } else if( no2 == IO_UNIT_UNKNOWN ){ return -1; } else { return no2 < no1; } } }