|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Goto Program |
| 4 | +
|
| 5 | +Author: Thomas Kiley |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include <goto-programs/show_goto_functions_xml.h> |
| 10 | +#include <iostream> |
| 11 | +#include <sstream> |
| 12 | + |
| 13 | + |
| 14 | +#include <util/xml_expr.h> |
| 15 | +#include <util/cprover_prefix.h> |
| 16 | +#include <util/prefix.h> |
| 17 | + |
| 18 | +#include <langapi/language_util.h> |
| 19 | + |
| 20 | +#include "goto_functions.h" |
| 21 | +#include "goto_model.h" |
| 22 | + |
| 23 | +/*******************************************************************\ |
| 24 | +
|
| 25 | +Function: show_goto_functions_xmlt::show_goto_functions_xmlt |
| 26 | +
|
| 27 | + Inputs: |
| 28 | + ns - the namespace to use to resolve names with |
| 29 | +
|
| 30 | + Outputs: |
| 31 | +
|
| 32 | + Purpose: For outputing the GOTO program in a readable xml format. |
| 33 | +
|
| 34 | +\*******************************************************************/ |
| 35 | + |
| 36 | +show_goto_functions_xmlt::show_goto_functions_xmlt(const namespacet &ns) |
| 37 | + : ns(ns) |
| 38 | +{} |
| 39 | + |
| 40 | +/*******************************************************************\ |
| 41 | +
|
| 42 | +Function: show_goto_functions_xmlt::show_goto_functions |
| 43 | +
|
| 44 | + Inputs: |
| 45 | + goto_functions - the goto functions that make up the program |
| 46 | +
|
| 47 | + Outputs: |
| 48 | +
|
| 49 | + Purpose: Walks through all of the functions in the program and returns |
| 50 | + an xml object representing all their functions. Produces output |
| 51 | + like this: |
| 52 | + <functions> |
| 53 | + <function name=main, isBodyAvaliable=true, isInternal=false> |
| 54 | + <instructions> |
| 55 | + <instruction id=ASSIGN> |
| 56 | + <location file="main.c" line="14"/> |
| 57 | + <instructionValue>// adsakjsdl |
| 58 | + x=y</instructionValue> |
| 59 | + </instruction> |
| 60 | + </instructions> |
| 61 | + </function> |
| 62 | + </functions> |
| 63 | +
|
| 64 | +\*******************************************************************/ |
| 65 | + |
| 66 | +xmlt show_goto_functions_xmlt::get_goto_functions( |
| 67 | + const goto_functionst &goto_functions) |
| 68 | +{ |
| 69 | + xmlt xml_functions=xmlt("functions"); |
| 70 | + for(const auto &function_entry : goto_functions.function_map) |
| 71 | + { |
| 72 | + const irep_idt &function_name=function_entry.first; |
| 73 | + const goto_functionst::goto_functiont &function=function_entry.second; |
| 74 | + |
| 75 | + xmlt &xml_function=xml_functions.new_element("function"); |
| 76 | + xml_function.set_attribute("name", id2string(function_name)); |
| 77 | + xml_function.set_attribute_bool( |
| 78 | + "isBodyAvailable", function.body_available()); |
| 79 | + bool is_internal=(has_prefix(id2string(function_name), CPROVER_PREFIX) || |
| 80 | + function_name==ID__start); |
| 81 | + xml_function.set_attribute_bool("isInternal", is_internal); |
| 82 | + |
| 83 | + if(function.body_available()) |
| 84 | + { |
| 85 | + xmlt &xml_instructions=xml_function.new_element("instructions"); |
| 86 | + for(const goto_programt::instructiont &instruction : |
| 87 | + function.body.instructions) |
| 88 | + { |
| 89 | + xmlt &instruction_entry=xml_instructions.new_element("instruction"); |
| 90 | + |
| 91 | + std::ostringstream instruction_id_builder; |
| 92 | + instruction_id_builder << instruction.type; |
| 93 | + |
| 94 | + instruction_entry.set_attribute( |
| 95 | + "instructionId", instruction_id_builder.str()); |
| 96 | + |
| 97 | + if(instruction.code.source_location().is_not_nil()) |
| 98 | + { |
| 99 | + instruction_entry.new_element( |
| 100 | + xml(instruction.code.source_location())); |
| 101 | + } |
| 102 | + |
| 103 | + std::ostringstream instruction_builder; |
| 104 | + function.body.output_instruction( |
| 105 | + ns, function_name, instruction_builder, instruction); |
| 106 | + |
| 107 | + xmlt &instruction_value= |
| 108 | + instruction_entry.new_element("instructionValue"); |
| 109 | + instruction_value.data=instruction_builder.str(); |
| 110 | + instruction_value.elements.clear(); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return xml_functions; |
| 115 | +} |
| 116 | + |
| 117 | +/*******************************************************************\ |
| 118 | +
|
| 119 | +Function: show_goto_functions_xmlt::print_goto_functions |
| 120 | +
|
| 121 | + Inputs: |
| 122 | + goto_functions - the goto functions that make up the program |
| 123 | + out - the stream to write the object to |
| 124 | + append - should a command and newline be appended to the stream |
| 125 | + before writing the xml object. Defaults to true |
| 126 | +
|
| 127 | + Outputs: |
| 128 | +
|
| 129 | + Purpose: Print the xml object generated by |
| 130 | + show_goto_functions_xmlt::show_goto_functions to the provided |
| 131 | + stream (e.g. std::cout) |
| 132 | +
|
| 133 | +\*******************************************************************/ |
| 134 | + |
| 135 | +void show_goto_functions_xmlt::print_goto_functions( |
| 136 | + const goto_functionst &goto_functions, |
| 137 | + std::ostream &out, |
| 138 | + bool append /*=true*/) |
| 139 | +{ |
| 140 | + const xmlt &function_output=get_goto_functions(goto_functions); |
| 141 | + if(append) |
| 142 | + { |
| 143 | + out << "\n"; |
| 144 | + } |
| 145 | + out << get_goto_functions(goto_functions); |
| 146 | +} |
0 commit comments