CBMC
show_symbol_table.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Show the symbol table
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "show_symbol_table.h"
13 
14 #include <iostream>
15 #include <memory>
16 
17 #include <langapi/language.h>
18 #include <langapi/mode.h>
19 
20 #include <util/json_irep.h>
21 #include <util/json_stream.h>
22 #include <util/ui_message.h>
23 
24 #include "goto_model.h"
25 
34 static std::unique_ptr<languaget>
36 {
37  if(!symbol.mode.empty())
38  if(auto language_from_mode = get_language_from_mode(symbol.mode))
39  return language_from_mode;
40  return get_default_language();
41 }
42 
44 {
45 }
46 
48  const symbol_table_baset &symbol_table,
49  std::ostream &out)
50 {
51  // we want to sort alphabetically
52  const namespacet ns(symbol_table);
53 
54  for(const auto &id : symbol_table.sorted_symbol_names())
55  {
56  const symbolt &symbol=ns.lookup(id);
57 
58  const std::unique_ptr<languaget> ptr = get_show_symbol_language(symbol);
59 
60  std::string type_str;
61 
62  if(symbol.type.is_not_nil())
63  ptr->from_type(symbol.type, type_str, ns);
64 
65  out << symbol.name << " " << type_str << '\n';
66  }
67 }
68 
70  const symbol_table_baset &symbol_table,
71  std::ostream &out)
72 {
73  out << '\n' << "Symbols:" << '\n' << '\n';
74 
75  const namespacet ns(symbol_table);
76 
77  // we want to sort alphabetically
78  for(const irep_idt &id : symbol_table.sorted_symbol_names())
79  {
80  const symbolt &symbol=ns.lookup(id);
81 
82  const std::unique_ptr<languaget> ptr = get_show_symbol_language(symbol);
83 
84  std::string type_str, value_str;
85 
86  if(symbol.type.is_not_nil())
87  ptr->from_type(symbol.type, type_str, ns);
88 
89  if(symbol.value.is_not_nil())
90  ptr->from_expr(symbol.value, value_str, ns);
91 
92  out << "Symbol......: " << symbol.name << '\n' << std::flush;
93  out << "Pretty name.: " << symbol.pretty_name << '\n';
94  out << "Module......: " << symbol.module << '\n';
95  out << "Base name...: " << symbol.base_name << '\n';
96  out << "Mode........: " << symbol.mode << '\n';
97  out << "Type........: " << type_str << '\n';
98  out << "Value.......: " << value_str << '\n';
99  out << "Flags.......:";
100 
101  if(symbol.is_lvalue)
102  out << " lvalue";
103  if(symbol.is_static_lifetime)
104  out << " static_lifetime";
105  if(symbol.is_thread_local)
106  out << " thread_local";
107  if(symbol.is_file_local)
108  out << " file_local";
109  if(symbol.is_type)
110  out << " type";
111  if(symbol.is_extern)
112  out << " extern";
113  if(symbol.is_input)
114  out << " input";
115  if(symbol.is_output)
116  out << " output";
117  if(symbol.is_macro)
118  out << " macro";
119  if(symbol.is_parameter)
120  out << " parameter";
121  if(symbol.is_auxiliary)
122  out << " auxiliary";
123  if(symbol.is_weak)
124  out << " weak";
125  if(symbol.is_property)
126  out << " property";
127  if(symbol.is_state_var)
128  out << " state_var";
129  if(symbol.is_exported)
130  out << " exported";
131  if(symbol.is_volatile)
132  out << " volatile";
133 
134  out << '\n';
135  out << "Location....: " << symbol.location << '\n';
136 
137  out << '\n' << std::flush;
138  }
139 }
140 
142  const symbol_table_baset &symbol_table,
143  ui_message_handlert &message_handler)
144 {
145  json_stream_arrayt &out = message_handler.get_json_stream();
146 
147  json_stream_objectt &result_wrapper = out.push_back_stream_object();
148  json_stream_objectt &result =
149  result_wrapper.push_back_stream_object("symbolTable");
150 
151  const namespacet ns(symbol_table);
152  json_irept irep_converter(true);
153 
154  for(const auto &id_and_symbol : symbol_table.symbols)
155  {
156  const symbolt &symbol = id_and_symbol.second;
157 
158  const std::unique_ptr<languaget> ptr = get_show_symbol_language(symbol);
159 
160  std::string type_str, value_str;
161 
162  if(symbol.type.is_not_nil())
163  ptr->from_type(symbol.type, type_str, ns);
164 
165  if(symbol.value.is_not_nil())
166  ptr->from_expr(symbol.value, value_str, ns);
167 
168  json_objectt symbol_json{
169  {"prettyName", json_stringt(symbol.pretty_name)},
170  {"name", json_stringt(symbol.name)},
171  {"baseName", json_stringt(symbol.base_name)},
172  {"mode", json_stringt(symbol.mode)},
173  {"module", json_stringt(symbol.module)},
174 
175  {"prettyType", json_stringt(type_str)},
176  {"prettyValue", json_stringt(value_str)},
177 
178  {"type", irep_converter.convert_from_irep(symbol.type)},
179  {"value", irep_converter.convert_from_irep(symbol.value)},
180  {"location", irep_converter.convert_from_irep(symbol.location)},
181 
182  {"isType", jsont::json_boolean(symbol.is_type)},
183  {"isMacro", jsont::json_boolean(symbol.is_macro)},
184  {"isExported", jsont::json_boolean(symbol.is_exported)},
185  {"isInput", jsont::json_boolean(symbol.is_input)},
186  {"isOutput", jsont::json_boolean(symbol.is_output)},
187  {"isStateVar", jsont::json_boolean(symbol.is_state_var)},
188  {"isProperty", jsont::json_boolean(symbol.is_property)},
189  {"isStaticLifetime", jsont::json_boolean(symbol.is_static_lifetime)},
190  {"isThreadLocal", jsont::json_boolean(symbol.is_thread_local)},
191  {"isLvalue", jsont::json_boolean(symbol.is_lvalue)},
192  {"isFileLocal", jsont::json_boolean(symbol.is_file_local)},
193  {"isExtern", jsont::json_boolean(symbol.is_extern)},
194  {"isVolatile", jsont::json_boolean(symbol.is_volatile)},
195  {"isParameter", jsont::json_boolean(symbol.is_parameter)},
196  {"isAuxiliary", jsont::json_boolean(symbol.is_auxiliary)},
197  {"isWeak", jsont::json_boolean(symbol.is_weak)}};
198 
199  result.push_back(id2string(symbol.name), std::move(symbol_json));
200  }
201 }
202 
204  const symbol_table_baset &symbol_table,
205  ui_message_handlert &message_handler)
206 {
207  json_stream_arrayt &out = message_handler.get_json_stream();
208 
209  json_stream_objectt &result_wrapper = out.push_back_stream_object();
210  json_stream_objectt &result =
211  result_wrapper.push_back_stream_object("symbolTable");
212 
213  const namespacet ns(symbol_table);
214  json_irept irep_converter(true);
215 
216  for(const auto &id_and_symbol : symbol_table.symbols)
217  {
218  const symbolt &symbol = id_and_symbol.second;
219 
220  const std::unique_ptr<languaget> ptr = get_show_symbol_language(symbol);
221 
222  std::string type_str, value_str;
223 
224  if(symbol.type.is_not_nil())
225  ptr->from_type(symbol.type, type_str, ns);
226 
227  json_objectt symbol_json{
228  {"prettyName", json_stringt(symbol.pretty_name)},
229  {"baseName", json_stringt(symbol.base_name)},
230  {"mode", json_stringt(symbol.mode)},
231  {"module", json_stringt(symbol.module)},
232 
233  {"prettyType", json_stringt(type_str)},
234 
235  {"type", irep_converter.convert_from_irep(symbol.type)}};
236 
237  result.push_back(id2string(symbol.name), std::move(symbol_json));
238  }
239 }
240 
242  const symbol_table_baset &symbol_table,
244 {
245  switch(ui.get_ui())
246  {
248  show_symbol_table_plain(symbol_table, std::cout);
249  break;
250 
253  break;
254 
256  show_symbol_table_json_ui(symbol_table, ui);
257  break;
258  }
259 }
260 
262  const goto_modelt &goto_model,
264 {
265  show_symbol_table(goto_model.symbol_table, ui);
266 }
267 
269  const symbol_table_baset &symbol_table,
271 {
272  switch(ui.get_ui())
273  {
275  show_symbol_table_brief_plain(symbol_table, std::cout);
276  break;
277 
280  break;
281 
283  show_symbol_table_brief_json_ui(symbol_table, ui);
284  break;
285  }
286 }
287 
289  const goto_modelt &goto_model,
291 {
292  show_symbol_table_brief(goto_model.symbol_table, ui);
293 }
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
bool empty() const
Definition: dstring.h:89
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:31
bool is_not_nil() const
Definition: irep.h:368
json_objectt convert_from_irep(const irept &) const
To convert to JSON from an irep structure by recursively generating JSON for the different sub trees.
Definition: json_irep.cpp:31
Provides methods for streaming JSON arrays.
Definition: json_stream.h:93
json_stream_objectt & push_back_stream_object()
Add a JSON object child stream.
Definition: json_stream.cpp:82
Provides methods for streaming JSON objects.
Definition: json_stream.h:140
void push_back(const std::string &key, const jsont &json)
Push back a JSON element into the current object stream.
Definition: json_stream.h:178
json_stream_objectt & push_back_stream_object(const std::string &key)
Add a JSON object stream for a specific key.
static jsont json_boolean(bool value)
Definition: json.h:97
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:148
The symbol table base class interface.
std::vector< irep_idt > sorted_symbol_names() const
Build and return a lexicographically sorted vector of symbol names from all symbols stored in this sy...
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Symbol table entry.
Definition: symbol.h:28
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
bool is_auxiliary
Definition: symbol.h:77
bool is_volatile
Definition: symbol.h:75
bool is_extern
Definition: symbol.h:74
bool is_macro
Definition: symbol.h:62
bool is_file_local
Definition: symbol.h:73
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
bool is_static_lifetime
Definition: symbol.h:70
bool is_property
Definition: symbol.h:67
bool is_parameter
Definition: symbol.h:76
bool is_thread_local
Definition: symbol.h:71
bool is_type
Definition: symbol.h:61
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
bool is_state_var
Definition: symbol.h:66
bool is_output
Definition: symbol.h:65
typet type
Type of symbol.
Definition: symbol.h:31
bool is_weak
Definition: symbol.h:78
irep_idt name
The unique identifier.
Definition: symbol.h:40
bool is_exported
Definition: symbol.h:63
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:52
bool is_lvalue
Definition: symbol.h:72
exprt value
Initial value of symbol.
Definition: symbol.h:34
irep_idt mode
Language mode.
Definition: symbol.h:49
bool is_input
Definition: symbol.h:64
virtual uit get_ui() const
Definition: ui_message.h:33
virtual json_stream_arrayt & get_json_stream()
Definition: ui_message.h:40
Symbol Table + CFG.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:40
Abstract interface to support a programming language.
std::unique_ptr< languaget > get_language_from_mode(const irep_idt &mode)
Get the language corresponding to the given mode.
Definition: mode.cpp:51
std::unique_ptr< languaget > get_default_language()
Returns the default language.
Definition: mode.cpp:139
void show_symbol_table_brief_plain(const symbol_table_baset &symbol_table, std::ostream &out)
void show_symbol_table(const symbol_table_baset &symbol_table, ui_message_handlert &ui)
static void show_symbol_table_json_ui(const symbol_table_baset &symbol_table, ui_message_handlert &message_handler)
void show_symbol_table_plain(const symbol_table_baset &symbol_table, std::ostream &out)
static void show_symbol_table_brief_json_ui(const symbol_table_baset &symbol_table, ui_message_handlert &message_handler)
void show_symbol_table_xml_ui()
static std::unique_ptr< languaget > get_show_symbol_language(const symbolt &symbol)
Gets the language which should be used for showing the type and value of the supplied symbol.
void show_symbol_table_brief(const symbol_table_baset &symbol_table, ui_message_handlert &ui)
Show the symbol table.