CBMC
symtab2gb_parse_options.cpp
Go to the documentation of this file.
1 /******************************************************************\
2 
3 Module: symtab2gb_parse_options
4 
5 Author: Diffblue Ltd.
6 
7 \******************************************************************/
8 
10 
11 #include <fstream>
12 #include <iostream>
13 #include <string>
14 
15 #include <ansi-c/ansi_c_language.h>
16 
20 
22 #include <langapi/mode.h>
23 
24 #include <linking/linking.h>
25 
26 #include <util/config.h>
27 #include <util/exception_utils.h>
28 #include <util/exit_codes.h>
29 #include <util/version.h>
30 
33  argc,
34  argv,
35  std::string("SYMTAB2GB ") + CBMC_VERSION}
36 {
37 }
38 
39 static inline bool failed(bool error_indicator)
40 {
41  return error_indicator;
42 }
43 
44 static void run_symtab2gb(
45  const std::vector<std::string> &symtab_filenames,
46  const std::string &gb_filename)
47 {
48  // try opening all the files first to make sure we can
49  // even read/write what we want
50  std::ofstream out_file{gb_filename, std::ios::binary};
51  if(!out_file.is_open())
52  {
53  throw system_exceptiont{"couldn't open output file '" + gb_filename + "'"};
54  }
55  std::vector<std::ifstream> symtab_files;
56  for(auto const &symtab_filename : symtab_filenames)
57  {
58  std::ifstream symtab_file{symtab_filename};
59  if(!symtab_file.is_open())
60  {
61  throw system_exceptiont{"couldn't open input file '" + symtab_filename +
62  "'"};
63  }
64  symtab_files.emplace_back(std::move(symtab_file));
65  }
66 
67  stream_message_handlert message_handler{std::cerr};
68 
69  auto const symtab_language = new_json_symtab_language();
70  symtab_language->set_message_handler(message_handler);
71 
72  symbol_tablet linked_symbol_table;
73 
74  for(std::size_t ix = 0; ix < symtab_files.size(); ++ix)
75  {
76  auto const &symtab_filename = symtab_filenames[ix];
77  auto &symtab_file = symtab_files[ix];
78  if(failed(symtab_language->parse(symtab_file, symtab_filename)))
79  {
80  source_locationt source_location;
81  source_location.set_file(symtab_filename);
83  "failed to parse symbol table", source_location};
84  }
85  symbol_tablet symtab{};
86  if(failed(symtab_language->typecheck(symtab, "<unused>")))
87  {
88  source_locationt source_location;
89  source_location.set_file(symtab_filename);
91  "failed to typecheck symbol table", source_location};
92  }
94 
95  if(failed(linking(linked_symbol_table, symtab, message_handler)))
96  {
98  "failed to link `" + symtab_filename + "'"};
99  }
100  }
101 
102  goto_modelt linked_goto_model;
103  linked_goto_model.symbol_table.swap(linked_symbol_table);
104  goto_convert(linked_goto_model, message_handler);
105 
106  if(failed(write_goto_binary(out_file, linked_goto_model)))
107  {
108  throw system_exceptiont{"failed to write goto binary to " + gb_filename};
109  }
110 }
111 
113 {
114  // As this is a converter and linker it only really needs to support
115  // the JSON symtab front-end.
117  // Workaround to allow external front-ends to use "C" mode
119 }
120 
122 {
123  if(cmdline.isset("version"))
124  {
125  log.status() << CBMC_VERSION << '\n';
126  return CPROVER_EXIT_SUCCESS;
127  }
128  if(cmdline.args.empty())
129  {
131  "expect at least one input file", "<json-symtab-file>"};
132  }
133  std::vector<std::string> symtab_filenames = cmdline.args;
134  std::string gb_filename = "a.out";
136  {
138  }
140  config.set(cmdline);
141  run_symtab2gb(symtab_filenames, gb_filename);
142  return CPROVER_EXIT_SUCCESS;
143 }
144 
146 {
147  log.status()
148  << '\n'
149  << banner_string("symtab2gb", CBMC_VERSION) << '\n'
150  << align_center_with_border("Copyright (C) 2019") << '\n'
151  << align_center_with_border("Diffblue Ltd.") << '\n'
152  << align_center_with_border("info@diffblue.com") << '\n'
153  << '\n'
154  << "Usage: Purpose:\n"
155  << '\n'
156  << "symtab2gb [-?] [-h] [--help] show help\n"
157  "symtab2gb compile .json_symtabs\n"
158  " <json-symtab-file>+ to a single goto-binary\n"
159  " [--out <outfile>]\n\n"
160  "<json-symtab-file> a CBMC symbol table in\n"
161  " JSON format\n"
162  "--out <outfile> specify the filename of\n"
163  " the resulting binary\n"
164  " (default: a.out)\n"
165  << messaget::eom;
166 }
cmdlinet::args
argst args
Definition: cmdline.h:145
exception_utils.h
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
linking
bool linking(symbol_tablet &dest_symbol_table, const symbol_tablet &new_symbol_table, message_handlert &message_handler)
Merges the symbol table new_symbol_table into dest_symbol_table, renaming symbols from new_symbol_tab...
Definition: linking.cpp:1475
parse_options_baset
Definition: parse_options.h:19
cmdlinet::isset
virtual bool isset(char option) const
Definition: cmdline.cpp:30
messaget::status
mstreamt & status() const
Definition: message.h:414
write_goto_binary
bool write_goto_binary(std::ostream &out, const symbol_tablet &symbol_table, const goto_functionst &goto_functions, irep_serializationt &irepconverter)
Writes a goto program to disc, using goto binary format.
Definition: write_goto_binary.cpp:24
invalid_input_exceptiont
Thrown when user-provided input cannot be processed.
Definition: exception_utils.h:162
symtab2gb_parse_optionst::doit
int doit() override
Definition: symtab2gb_parse_options.cpp:121
goto_model.h
goto_modelt
Definition: goto_model.h:25
mode.h
invalid_source_file_exceptiont
Thrown when we can't handle something in an input source file.
Definition: exception_utils.h:171
messaget::eom
static eomt eom
Definition: message.h:297
symtab2gb_parse_optionst::help
void help() override
Definition: symtab2gb_parse_options.cpp:145
version.h
write_goto_binary.h
goto_convert
void goto_convert(const codet &code, symbol_table_baset &symbol_table, goto_programt &dest, message_handlert &message_handler, const irep_idt &mode)
Definition: goto_convert.cpp:1905
CBMC_VERSION
const char * CBMC_VERSION
new_json_symtab_language
std::unique_ptr< languaget > new_json_symtab_language()
Definition: json_symtab_language.h:75
symtab2gb_parse_options.h
symtab2gb_parse_optionst::symtab2gb_parse_optionst
symtab2gb_parse_optionst(int argc, const char *argv[])
Definition: symtab2gb_parse_options.cpp:31
banner_string
std::string banner_string(const std::string &front_end, const std::string &version)
Definition: parse_options.cpp:174
failed
static bool failed(bool error_indicator)
Definition: symtab2gb_parse_options.cpp:39
source_locationt::set_file
void set_file(const irep_idt &file)
Definition: source_location.h:90
cmdlinet::get_value
std::string get_value(char option) const
Definition: cmdline.cpp:48
system_exceptiont
Thrown when some external system fails unexpectedly.
Definition: exception_utils.h:71
SYMTAB2GB_OPTIONS
#define SYMTAB2GB_OPTIONS
Definition: symtab2gb_parse_options.h:18
linking.h
configt::set_from_symbol_table
void set_from_symbol_table(const symbol_tablet &)
Definition: config.cpp:1254
config
configt config
Definition: config.cpp:25
source_locationt
Definition: source_location.h:18
parse_options_baset::log
messaget log
Definition: parse_options.h:46
symtab2gb_parse_optionst::register_languages
void register_languages() override
Definition: symtab2gb_parse_options.cpp:112
register_language
void register_language(language_factoryt factory)
Register a language Note: registering a language is required for using the functions in language_util...
Definition: mode.cpp:39
ansi_c_language.h
symbol_tablet::swap
void swap(symbol_tablet &other)
Swap symbol maps between two symbol tables.
Definition: symbol_table.h:74
configt::set
bool set(const cmdlinet &cmdline)
Definition: config.cpp:798
exit_codes.h
new_ansi_c_language
std::unique_ptr< languaget > new_ansi_c_language()
Definition: ansi_c_language.cpp:150
binary
static std::string binary(const constant_exprt &src)
Definition: json_expr.cpp:189
config.h
run_symtab2gb
static void run_symtab2gb(const std::vector< std::string > &symtab_filenames, const std::string &gb_filename)
Definition: symtab2gb_parse_options.cpp:44
goto_convert_functions.h
CPROVER_EXIT_SUCCESS
#define CPROVER_EXIT_SUCCESS
Success indicates the required analysis has been performed without error.
Definition: exit_codes.h:16
invalid_command_line_argument_exceptiont
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
Definition: exception_utils.h:50
parse_options_baset::cmdline
cmdlinet cmdline
Definition: parse_options.h:28
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
json_symtab_language.h
align_center_with_border
std::string align_center_with_border(const std::string &text)
Utility for displaying help centered messages borderered by "* *".
Definition: parse_options.cpp:161
SYMTAB2GB_OUT_FILE_OPT
#define SYMTAB2GB_OUT_FILE_OPT
Definition: symtab2gb_parse_options.h:14
stream_message_handlert
Definition: message.h:110