cprover
language_ui.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@cs.cmu.edu
6 
7 \*******************************************************************/
8 
9 #include "language_ui.h"
10 
11 #include <fstream>
12 #include <memory>
13 #include <iostream>
14 
15 #include <util/cmdline.h>
16 #include <util/config.h>
17 #include <util/namespace.h>
18 #include <util/options.h>
19 #include <util/unicode.h>
20 
21 #include "language.h"
22 #include "mode.h"
23 
26  const cmdlinet &cmdline,
27  ui_message_handlert &_ui_message_handler,
28  optionst *_options)
29  : _cmdline(cmdline),
30  ui_message_handler(_ui_message_handler),
31  options(_options)
32 {
34 }
35 
38 {
39 }
40 
42 {
43  for(const auto &filename : _cmdline.args)
44  if(parse(filename))
45  return true;
46 
47  return false;
48 }
49 
50 bool language_uit::parse(const std::string &filename)
51 {
52  #ifdef _MSC_VER
53  std::ifstream infile(widen(filename));
54  #else
55  std::ifstream infile(filename);
56  #endif
57 
58  if(!infile)
59  {
60  error() << "failed to open input file `" << filename << "'" << eom;
61  return true;
62  }
63 
66 
67  if(lf.language==nullptr)
68  {
69  source_locationt location;
70  location.set_file(filename);
71  error().source_location=location;
72  error() << "failed to figure out type of file" << eom;
73  return true;
74  }
75 
76  languaget &language=*lf.language;
78 
79  if(options != nullptr)
80  language.set_language_options(*options);
81 
82  status() << "Parsing " << filename << eom;
83 
84  if(language.parse(infile, filename))
85  {
87  std::cerr << "PARSING ERROR\n";
88 
89  return true;
90  }
91 
92  lf.get_modules();
93 
94  return false;
95 }
96 
98 {
99  status() << "Converting" << eom;
100 
102 
104  {
105  error() << "CONVERSION ERROR" << eom;
106  return true;
107  }
108 
109  return false;
110 }
111 
113 {
115 
117  {
118  error() << "CONVERSION ERROR" << eom;
119  return true;
120  }
121 
123 
124  return false;
125 }
126 
128 {
129  switch(get_ui())
130  {
132  show_symbol_table_plain(std::cout, brief);
133  break;
134 
137  break;
138 
139  default:
140  error() << "cannot show symbol table in this format" << eom;
141  }
142 }
143 
145 {
146  error() << "cannot show symbol table in this format" << eom;
147 }
148 
150  std::ostream &out,
151  bool brief)
152 {
153  if(!brief)
154  out << "\nSymbols:\n\n";
155 
156  // we want to sort alphabetically
157  std::set<std::string> symbols;
158 
159  for(const auto &symbol_pair : symbol_table.symbols)
160  {
161  symbols.insert(id2string(symbol_pair.first));
162  }
163 
164  const namespacet ns(symbol_table);
165 
166  for(const std::string &id : symbols)
167  {
168  const symbolt &symbol=ns.lookup(id);
169 
170  std::unique_ptr<languaget> ptr;
171 
172  if(symbol.mode=="")
173  {
174  ptr=get_default_language();
175  }
176  else
177  {
178  ptr=get_language_from_mode(symbol.mode);
179  }
180 
181  if(!ptr)
182  throw "symbol "+id2string(symbol.name)+" has unknown mode";
183 
184  std::string type_str, value_str;
185 
186  if(symbol.type.is_not_nil())
187  ptr->from_type(symbol.type, type_str, ns);
188 
189  if(symbol.value.is_not_nil())
190  ptr->from_expr(symbol.value, value_str, ns);
191 
192  if(brief)
193  {
194  out << symbol.name << " " << type_str << '\n';
195  continue;
196  }
197 
198  out << "Symbol......: " << symbol.name << '\n' << std::flush;
199  out << "Pretty name.: " << symbol.pretty_name << '\n';
200  out << "Module......: " << symbol.module << '\n';
201  out << "Base name...: " << symbol.base_name << '\n';
202  out << "Mode........: " << symbol.mode << '\n';
203  out << "Type........: " << type_str << '\n';
204  out << "Value.......: " << value_str << '\n';
205  out << "Flags.......:";
206 
207  if(symbol.is_lvalue)
208  out << " lvalue";
209  if(symbol.is_static_lifetime)
210  out << " static_lifetime";
211  if(symbol.is_thread_local)
212  out << " thread_local";
213  if(symbol.is_file_local)
214  out << " file_local";
215  if(symbol.is_type)
216  out << " type";
217  if(symbol.is_extern)
218  out << " extern";
219  if(symbol.is_input)
220  out << " input";
221  if(symbol.is_output)
222  out << " output";
223  if(symbol.is_macro)
224  out << " macro";
225  if(symbol.is_parameter)
226  out << " parameter";
227  if(symbol.is_auxiliary)
228  out << " auxiliary";
229  if(symbol.is_weak)
230  out << " weak";
231  if(symbol.is_property)
232  out << " property";
233  if(symbol.is_state_var)
234  out << " state_var";
235  if(symbol.is_exported)
236  out << " exported";
237  if(symbol.is_volatile)
238  out << " volatile";
239 
240  out << '\n';
241  out << "Location....: " << symbol.location << '\n';
242 
243  out << '\n' << std::flush;
244  }
245 }
symbol_tablet symbol_table
Definition: language_ui.h:25
irep_idt name
The unique identifier.
Definition: symbol.h:40
bool is_output
Definition: symbol.h:61
virtual ~language_uit()
Destructor.
Definition: language_ui.cpp:37
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
bool is_not_nil() const
Definition: irep.h:173
bool is_thread_local
Definition: symbol.h:65
bool final(symbol_table_baset &symbol_table)
virtual bool parse()
Definition: language_ui.cpp:41
std::wstring widen(const char *s)
Definition: unicode.cpp:52
irep_idt mode
Language mode.
Definition: symbol.h:49
optionst * options
Definition: language_ui.h:57
std::unique_ptr< languaget > get_default_language()
Returns the default language.
Definition: mode.cpp:138
std::unique_ptr< languaget > get_language_from_mode(const irep_idt &mode)
Get the language corresponding to the given mode.
Definition: mode.cpp:50
uit get_ui()
Definition: language_ui.h:49
language_filest language_files
Definition: language_ui.h:24
language_filet & add_file(const std::string &filename)
Definition: language_file.h:76
exprt value
Initial value of symbol.
Definition: symbol.h:34
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
virtual bool typecheck()
Definition: language_ui.cpp:97
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:52
const cmdlinet & _cmdline
Definition: language_ui.h:55
virtual void show_symbol_table(bool brief=false)
std::unique_ptr< languaget > get_language_from_filename(const std::string &filename)
Get the language corresponding to the registered file name extensions.
Definition: mode.cpp:101
Symbol table entry.
Definition: symbol.h:27
configt config
Definition: config.cpp:24
bool is_static_lifetime
Definition: symbol.h:65
bool is_input
Definition: symbol.h:61
virtual void show_symbol_table_xml_ui(bool brief)
std::unique_ptr< languaget > language
Definition: language_file.h:46
argst args
Definition: cmdline.h:44
bool is_exported
Definition: symbol.h:61
bool is_parameter
Definition: symbol.h:66
source_locationt source_location
Definition: message.h:236
void set_file(const irep_idt &file)
ui_message_handlert & ui_message_handler
Definition: language_ui.h:56
mstreamt & error() const
Definition: message.h:386
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:93
Abstract interface to support a programming language.
bool typecheck(symbol_tablet &symbol_table)
virtual void set_message_handler(message_handlert &_message_handler)
Definition: message.h:169
const symbolst & symbols
bool is_volatile
Definition: symbol.h:66
bool is_extern
Definition: symbol.h:66
static eomt eom
Definition: message.h:284
virtual bool from_type(const typet &type, std::string &code, const namespacet &ns)
Formats the given type in a language-specific way.
Definition: language.cpp:46
virtual void show_symbol_table_plain(std::ostream &out, bool brief)
typet type
Type of symbol.
Definition: symbol.h:31
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
virtual bool final()
message_handlert & get_message_handler()
Definition: message.h:174
mstreamt & status() const
Definition: message.h:401
bool is_state_var
Definition: symbol.h:61
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
bool is_file_local
Definition: symbol.h:66
bool is_weak
Definition: symbol.h:66
virtual void set_language_options(const optionst &)
Set language-specific options.
Definition: language.h:43
language_uit(const cmdlinet &cmdline, ui_message_handlert &ui_message_handler, optionst *options=nullptr)
Constructor.
Definition: language_ui.cpp:25
Options.
bool is_auxiliary
Definition: symbol.h:66
virtual bool from_expr(const exprt &expr, std::string &code, const namespacet &ns)
Formats the given expression in a language-specific way.
Definition: language.cpp:37
bool is_type
Definition: symbol.h:61
virtual bool parse(std::istream &instream, const std::string &path)=0
bool is_property
Definition: symbol.h:61
message_handlert * message_handler
Definition: message.h:426
bool is_macro
Definition: symbol.h:61
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:166
void set_object_bits_from_symbol_table(const symbol_tablet &)
Sets the number of bits used for object addresses.
Definition: config.cpp:1232
bool is_lvalue
Definition: symbol.h:66