|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Get goto model |
| 4 | +
|
| 5 | +Author: Daniel Poetzl |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include "get_goto_model_from_c.h" |
| 10 | + |
| 11 | +#include <ansi-c/ansi_c_language.h> |
| 12 | + |
| 13 | +#include <goto-programs/goto_convert_functions.h> |
| 14 | + |
| 15 | +#include <langapi/language_file.h> |
| 16 | +#include <langapi/mode.h> |
| 17 | + |
| 18 | +#include <util/cmdline.h> |
| 19 | +#include <util/config.h> |
| 20 | +#include <util/exception_utils.h> |
| 21 | +#include <util/invariant.h> |
| 22 | +#include <util/message.h> |
| 23 | +#include <util/options.h> |
| 24 | +#include <util/symbol_table.h> |
| 25 | + |
| 26 | +#include <testing-utils/message.h> |
| 27 | + |
| 28 | +goto_modelt get_goto_model_from_c(std::istream &in) |
| 29 | +{ |
| 30 | + { |
| 31 | + const bool has_language = get_language_from_mode(ID_C) != nullptr; |
| 32 | + |
| 33 | + if(!has_language) |
| 34 | + { |
| 35 | + register_language(new_ansi_c_language); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + { |
| 40 | + cmdlinet cmdline; |
| 41 | + |
| 42 | + config = configt{}; |
| 43 | + config.main = std::string("main"); |
| 44 | + config.set(cmdline); |
| 45 | + } |
| 46 | + |
| 47 | + language_filest language_files; |
| 48 | + language_files.set_message_handler(null_message_handler); |
| 49 | + |
| 50 | + language_filet &language_file = language_files.add_file(""); |
| 51 | + |
| 52 | + language_file.language = get_language_from_mode(ID_C); |
| 53 | + CHECK_RETURN(language_file.language); |
| 54 | + |
| 55 | + languaget &language = *language_file.language; |
| 56 | + language.set_message_handler(null_message_handler); |
| 57 | + |
| 58 | + { |
| 59 | + const bool error = language.parse(in, ""); |
| 60 | + |
| 61 | + if(error) |
| 62 | + throw invalid_source_file_exceptiont("parsing failed"); |
| 63 | + } |
| 64 | + |
| 65 | + language_file.get_modules(); |
| 66 | + |
| 67 | + goto_modelt goto_model; |
| 68 | + |
| 69 | + { |
| 70 | + const bool error = language_files.typecheck(goto_model.symbol_table); |
| 71 | + |
| 72 | + if(error) |
| 73 | + throw invalid_source_file_exceptiont("typechecking failed"); |
| 74 | + } |
| 75 | + |
| 76 | + { |
| 77 | + const bool error = |
| 78 | + language_files.generate_support_functions(goto_model.symbol_table); |
| 79 | + |
| 80 | + if(error) |
| 81 | + throw invalid_source_file_exceptiont( |
| 82 | + "support function generation failed"); |
| 83 | + } |
| 84 | + |
| 85 | + goto_convert( |
| 86 | + goto_model.symbol_table, goto_model.goto_functions, null_message_handler); |
| 87 | + |
| 88 | + return goto_model; |
| 89 | +} |
| 90 | + |
| 91 | +goto_modelt get_goto_model_from_c(const std::string &code) |
| 92 | +{ |
| 93 | + std::istringstream in(code); |
| 94 | + return get_goto_model_from_c(in); |
| 95 | +} |
0 commit comments