/*-------------------------------------------------------------------- * src_viewer::build_stree */ void src_viewer::build_stree() { /* create a new src tree */ stree = new code_tree; stree->set_map_fn((map_tn_fn) &map_tree_node, this); stree->id = new source_id( current_file_block, current_file_name ); stree_cache->push_back( stree ); post_progress(this, "Loading source file ...", 0); /* create link to source */ DefinitionBlock *def_block = current_file_block->get_definition_block(); int count = def_block->get_procedure_definition_count(); for ( int i = 0 ; i<count; i++ ) { ProcedureDefinition *proc = def_block->get_procedure_definition(i); ExecutionObject* body = proc->get_body(); if ( body ) { stree->build( proc ); } post_progress( this, "Loading SUIF procedures..", ((float) (i+1))/count*100); } unpost_progress( this ); }
/*-------------------------------------------------------------------- * output_viewer::print_output * */ bool output_viewer::print_output(String file) { const char* filename = file.c_str(); post_progress(this, "Loading output file ...", 0); text->clear(); FILE *fd = fopen( filename, "r" ); if (!fd) { text->fout() << "Cannot open file " << filename << endl; text->update(); unpost_progress( this ); return false; } /* read the file */ fseek(fd, 0, SEEK_END); long length = ftell(fd); fseek(fd, 0, SEEK_SET); char *buffer = new char[length]; int l = fread(buffer, 1, length, fd); buffer[l] = 0; fclose(fd); int current_line = 1; int current_src_line = -1; bool new_src_line = false; bool parse_error = false; int scope = 0; scope_node *current_scope = 0; int state = NORMAL_STATE; erase_mappings(); // erase previous mappings char *next_line; char *current_src_file = 0; for (char *line = buffer; line; line = next_line) { next_line = strchr(line, '\n'); if (next_line) { *next_line++ = 0; } /* * Preprocess C */ bool found_line_num; char *ppc = preprocess_c(line, state, current_src_file, current_src_line, found_line_num); /* * Scan for "{" and "}" to determine current scope. */ for (char *p = ppc; *p; p++) { if (*p == '{') { if (scope == 0) { current_scope = new scope_node; current_scope->first_line = current_line; current_scope->proc = 0; proc_scopes->push_back(current_scope); } scope++; } else if (*p == '}') { scope--; if (scope < 0) { parse_error = true; } if (scope == 0) { current_scope->last_line = current_line; } } } delete (ppc); /* Check if current line has line number annotation */ if (found_line_num) { new_src_line = true; continue; // don't print it out } /* * Record the line, if nec. */ if (new_src_line) { /* enter this line as a node */ output_node *node = new output_node(current_src_line, current_src_file, current_line); if (current_scope) { current_scope->nodes.push_back(node); /* match current scope to procedure */ if (current_scope->proc == 0) { SuifObject *tn = map_line_to_tree_node( find_file_block(String(current_src_file)), current_src_line); if (tn) { current_scope->proc = get_procedure_definition( tn ); } } } new_src_line = false; } /* print the line out */ text->fout() << line << endl; current_line++; } delete (buffer); text->update(); if (scope != 0 || parse_error) { display_message(this, "Warning: unable to parse the file `%s'.", filename); } if ( proc_scopes->empty()) { display_message(this, "Warning: cannot find line number information in " "the file `%s'.", filename); unpost_progress( this ); return false; } /* * Get the file set entry of this file */ if (!current_fse) { if ( current_src_file ) { current_fse = find_file_block(current_src_file); } if (!current_fse) { display_message(this, "Warning: cannot find the corresponding file set entry."); unpost_progress(this); return false; } } /* * Create new output tree */ delete outtree; outtree = new code_tree; outtree->set_map_fn( (map_tn_fn) &map_tree_node, this ); // iterate over the procedures DefinitionBlock *def_block = current_fse->get_definition_block(); int num_procs = def_block->get_procedure_definition_count(); for (int i = 0; i < num_procs; ++i) { ProcedureDefinition *proc = def_block->get_procedure_definition(i); ExecutionObject *body = proc->get_body(); if ( body ) { outtree->build( proc ); } post_progress(this, "Loading output file..", ((float) (i+1))/num_procs*100); } /* create tags */ outtree->create_tags(text); unpost_progress(this); return true; }