cprover
xml_irep.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening
6 
7  Date: November 2005
8 
9 \*******************************************************************/
10 
11 #include "xml_irep.h"
12 
13 #include <iostream>
14 #include <string>
15 
16 #include "irep.h"
17 #include "source_location.h"
18 
19 void convert(
20  const irept &irep,
21  xmlt &xml)
22 {
23  if(irep.id()!=ID_nil)
24  xml.new_element("id").data=irep.id_string();
25 
26  forall_irep(it, irep.get_sub())
27  {
28  xmlt &x_sub=xml.new_element("sub");
29  convert(*it, x_sub);
30  }
31 
33  if(!irept::is_comment(it->first))
34  {
35  xmlt &x_nsub = xml.new_element("named_sub");
36  x_nsub.set_attribute("name", name2string(it->first));
37  convert(it->second, x_nsub);
38  }
39 
41  if(!irept::is_comment(it->first))
42  {
43  xmlt &x_com = xml.new_element("comment");
44  x_com.set_attribute("name", name2string(it->first));
45  convert(it->second, x_com);
46  }
47 }
48 
49 void convert(
50  const xmlt &xml,
51  irept &irep)
52 {
53  irep.id(ID_nil);
54 
55  xmlt::elementst::const_iterator it = xml.elements.begin();
56  for(; it != xml.elements.end(); it++)
57  {
58  if(it->name=="id")
59  {
60  irep.id(it->data);
61  }
62  else if(it->name=="named_sub")
63  {
64  irept r;
65  convert(*it, r);
66  std::string named_name = it->get_attribute("name");
67  irep.move_to_named_sub(named_name, r);
68  }
69  else if(it->name=="sub")
70  {
71  irept r;
72  convert(*it, r);
73  irep.move_to_sub(r);
74  }
75  else if(it->name=="comment")
76  {
77  irept r;
78  convert(*it, r);
79  std::string named_name = it->get_attribute("name");
80  irep.move_to_named_sub(named_name, r);
81  }
82  else
83  {
84  // Should not happen
85  std::cout << "Unknown sub found (" << it->name << "); malformed xml?";
86  std::cout << "\n";
87  }
88  }
89 }
90 
91 xmlt xml(const source_locationt &location)
92 {
93  xmlt result;
94 
95  result.name = "location";
96 
97  if(!location.get_working_directory().empty())
98  result.set_attribute(
99  "working-directory", id2string(location.get_working_directory()));
100 
101  if(!location.get_file().empty())
102  result.set_attribute("file", id2string(location.get_file()));
103 
104  if(!location.get_line().empty())
105  result.set_attribute("line", id2string(location.get_line()));
106 
107  if(!location.get_column().empty())
108  result.set_attribute("column", id2string(location.get_column()));
109 
110  if(!location.get_function().empty())
111  result.set_attribute("function", id2string(location.get_function()));
112 
113  return result;
114 }
xmlt::elements
elementst elements
Definition: xml.h:42
source_locationt::get_function
const irep_idt & get_function() const
Definition: source_location.h:56
irept::is_comment
static bool is_comment(const irep_namet &name)
Definition: irep.h:489
irept::move_to_sub
void move_to_sub(irept &irep)
Definition: irep.cpp:42
source_locationt::get_column
const irep_idt & get_column() const
Definition: source_location.h:51
xml_irep.h
convert
void convert(const irept &irep, xmlt &xml)
Definition: xml_irep.cpp:19
source_locationt::get_line
const irep_idt & get_line() const
Definition: source_location.h:46
irept::move_to_named_sub
void move_to_named_sub(const irep_namet &name, irept &irep)
Definition: irep.cpp:33
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
irept::get_named_sub
named_subt & get_named_sub()
Definition: irep.h:479
xml
xmlt xml(const source_locationt &location)
Definition: xml_irep.cpp:91
xmlt::name
std::string name
Definition: xml.h:39
forall_named_irep
#define forall_named_irep(it, irep)
Definition: irep.h:70
irept::id_string
const std::string & id_string() const
Definition: irep.h:421
source_location.h
irept::id
const irep_idt & id() const
Definition: irep.h:418
dstringt::empty
bool empty() const
Definition: dstring.h:88
xmlt
Definition: xml.h:21
source_locationt
Definition: source_location.h:20
forall_irep
#define forall_irep(it, irep)
Definition: irep.h:62
xmlt::set_attribute
void set_attribute(const std::string &attribute, unsigned value)
Definition: xml.cpp:175
name2string
const std::string & name2string(const irep_namet &n)
Definition: irep.h:53
irept::get_sub
subt & get_sub()
Definition: irep.h:477
source_locationt::get_file
const irep_idt & get_file() const
Definition: source_location.h:36
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:394
r
static int8_t r
Definition: irep_hash.h:59
xmlt::data
std::string data
Definition: xml.h:39
source_locationt::get_working_directory
const irep_idt & get_working_directory() const
Definition: source_location.h:41
xmlt::new_element
xmlt & new_element(const std::string &key)
Definition: xml.h:95
irep.h