Skip to content

Commit 47982db

Browse files
author
thk123
committed
Added a to_string method to instructions
This way if you are not using an ostream you don't have to create one just to get the string. Replaced uses in show_goto_function_json and show_goto_functions_xml Since this isn't performance critical we use std::string as makes the result connect more cleanly to external APIs like the JSON and XML libraries.
1 parent f57ab77 commit 47982db

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/goto-programs/goto_program_template.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Author: Daniel Kroening, kroening@kroening.com
2020
#include <util/symbol_table.h>
2121
#include <util/source_location.h>
2222
#include <util/std_expr.h>
23+
#include <sstream>
24+
#include <string>
2325

2426
typedef enum { NO_INSTRUCTION_TYPE=0,
2527
GOTO=1, // branch, possibly guarded
@@ -240,6 +242,13 @@ class goto_program_templatet
240242

241243
return false;
242244
}
245+
246+
std::string to_string() const
247+
{
248+
std::ostringstream instruction_id_builder;
249+
instruction_id_builder << type;
250+
return instruction_id_builder.str();
251+
}
243252
};
244253

245254
typedef std::list<class instructiont> instructionst;

src/goto-programs/show_goto_functions_json.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,8 @@ json_objectt show_goto_functions_jsont::get_goto_functions(
7777
{
7878
json_objectt instruction_entry=json_objectt();
7979

80-
std::ostringstream instruction_id_builder;
81-
instruction_id_builder << instruction.type;
82-
8380
instruction_entry["instructionId"]=
84-
json_stringt(instruction_id_builder.str());
81+
json_stringt(instruction.to_string());
8582

8683
if(instruction.code.source_location().is_not_nil())
8784
{

src/goto-programs/show_goto_functions_xml.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,8 @@ xmlt show_goto_functions_xmlt::get_goto_functions(
8888
{
8989
xmlt &instruction_entry=xml_instructions.new_element("instruction");
9090

91-
std::ostringstream instruction_id_builder;
92-
instruction_id_builder << instruction.type;
93-
9491
instruction_entry.set_attribute(
95-
"instructionId", instruction_id_builder.str());
92+
"instructionId", instruction.to_string());
9693

9794
if(instruction.code.source_location().is_not_nil())
9895
{

0 commit comments

Comments
 (0)