Skip to content

Commit f57ab77

Browse files
author
thk123
committed
Basic implementation for the XML class
Produces an XML version of the show-goto-functions flag (i.e. when running a tool with the --xml-ui flag).
1 parent 25ba4d3 commit f57ab77

File tree

4 files changed

+181
-10
lines changed

4 files changed

+181
-10
lines changed

src/goto-programs/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ SRC = goto_convert.cpp goto_convert_function_call.cpp \
1717
goto_trace.cpp xml_goto_trace.cpp vcd_goto_trace.cpp \
1818
graphml_witness.cpp remove_virtual_functions.cpp \
1919
class_hierarchy.cpp show_goto_functions.cpp get_goto_model.cpp \
20-
slice_global_inits.cpp goto_inline_class.cpp show_goto_functions_json.cpp
20+
slice_global_inits.cpp goto_inline_class.cpp show_goto_functions_json.cpp \
21+
show_goto_functions_xml.cpp
2122

2223
INCLUDES= -I ..
2324

src/goto-programs/show_goto_functions.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Author: Peter Schrammel
1717

1818
#include <langapi/language_util.h>
1919
#include <goto-programs/show_goto_functions_json.h>
20+
#include <goto-programs/show_goto_functions_xml.h>
2021

2122
#include "show_goto_functions.h"
2223
#include "goto_functions.h"
@@ -43,15 +44,8 @@ void show_goto_functions(
4344
{
4445
case ui_message_handlert::XML_UI:
4546
{
46-
//This only prints the list of functions
47-
xmlt xml_functions("functions");
48-
forall_goto_functions(it, goto_functions)
49-
{
50-
xmlt &xml_function=xml_functions.new_element("function");
51-
xml_function.set_attribute("name", id2string(it->first));
52-
}
53-
54-
std::cout << xml_functions << std::endl;
47+
show_goto_functions_xmlt xml_show_functions(ns);
48+
xml_show_functions.print_goto_functions(goto_functions, std::cout);
5549
}
5650
break;
5751

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*******************************************************************\
2+
3+
Module: Goto Program
4+
5+
Author: Thomas Kiley
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_GOTO_PROGRAMS_SHOW_GOTO_FUNCTIONS_XML_H
10+
#define CPROVER_GOTO_PROGRAMS_SHOW_GOTO_FUNCTIONS_XML_H
11+
12+
#include <util/xml.h>
13+
14+
class goto_functionst;
15+
class namespacet;
16+
17+
class show_goto_functions_xmlt
18+
{
19+
public:
20+
explicit show_goto_functions_xmlt(const namespacet &ns);
21+
22+
xmlt get_goto_functions(const goto_functionst &goto_functions);
23+
void print_goto_functions(
24+
const goto_functionst &goto_functions, std::ostream &out, bool append=true);
25+
26+
private:
27+
const namespacet &ns;
28+
};
29+
30+
#endif // CPROVER_GOTO_PROGRAMS_SHOW_GOTO_FUNCTIONS_XML_H

0 commit comments

Comments
 (0)