Skip to content

Commit a207b8a

Browse files
Use structured exceptions in initialize_goto_model
1 parent 30119c5 commit a207b8a

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

src/goto-programs/initialize_goto_model.cpp

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <utility>
2+
13
/*******************************************************************\
24
35
Module: Get a Goto Program
@@ -22,6 +24,7 @@ Author: Daniel Kroening, [email protected]
2224
#include <langapi/language.h>
2325

2426
#include <goto-programs/rebuild_goto_start_function.h>
27+
#include <util/exception_utils.h>
2528

2629
#include "goto_convert_functions.h"
2730
#include "read_goto_binary.h"
@@ -34,8 +37,9 @@ goto_modelt initialize_goto_model(
3437
const std::vector<std::string> &files=cmdline.args;
3538
if(files.empty())
3639
{
37-
msg.error() << "Please provide a program" << messaget::eom;
38-
throw 0;
40+
throw invalid_user_input_exceptiont("missing program argument",
41+
"",
42+
"one or more paths to program files");
3943
}
4044

4145
std::vector<std::string> binaries, sources;
@@ -67,21 +71,18 @@ goto_modelt initialize_goto_model(
6771

6872
if(!infile)
6973
{
70-
msg.error() << "failed to open input file `" << filename
71-
<< '\'' << messaget::eom;
72-
throw 0;
74+
throw io_exceptiont("Failed to open input file `" + filename + '\'');
7375
}
7476

7577
language_filet &lf=language_files.add_file(filename);
7678
lf.language=get_language_from_filename(filename);
7779

7880
if(lf.language==nullptr)
7981
{
82+
8083
source_locationt location;
8184
location.set_file(filename);
82-
msg.error().source_location=location;
83-
msg.error() << "failed to figure out type of file" << messaget::eom;
84-
throw 0;
85+
throw goto_model_initialization_errort("Failed to figure out type of file", location);
8586
}
8687

8788
languaget &language=*lf.language;
@@ -92,8 +93,8 @@ goto_modelt initialize_goto_model(
9293

9394
if(language.parse(infile, filename))
9495
{
95-
msg.error() << "PARSING ERROR" << messaget::eom;
96-
throw 0;
96+
// FIXME not a super helpful message
97+
throw goto_model_initialization_errort("PARSING ERROR");
9798
}
9899

99100
lf.get_modules();
@@ -103,17 +104,17 @@ goto_modelt initialize_goto_model(
103104

104105
if(language_files.typecheck(goto_model.symbol_table))
105106
{
106-
msg.error() << "CONVERSION ERROR" << messaget::eom;
107-
throw 0;
107+
throw goto_model_initialization_errort("CONVERSION ERROR");
108108
}
109109
}
110110

111111
for(const auto &file : binaries)
112112
{
113113
msg.status() << "Reading GOTO program from file" << messaget::eom;
114114

115-
if(read_object_and_link(file, goto_model, message_handler))
116-
throw 0;
115+
if(read_object_and_link(file, goto_model, message_handler)) {
116+
throw goto_model_initialization_errort("failed to read object or link in file `" + file + '\'');
117+
}
117118
}
118119

119120
bool binaries_provided_start=
@@ -150,14 +151,13 @@ goto_modelt initialize_goto_model(
150151

151152
if(entry_point_generation_failed)
152153
{
153-
msg.error() << "SUPPORT FUNCTION GENERATION ERROR" << messaget::eom;
154-
throw 0;
154+
// FIXME more helpful error message?
155+
throw goto_model_initialization_errort("SUPPORT FUNCTION GENERATION ERROR");
155156
}
156157

157158
if(language_files.final(goto_model.symbol_table))
158159
{
159-
msg.error() << "FINAL STAGE CONVERSION ERROR" << messaget::eom;
160-
throw 0;
160+
throw goto_model_initialization_errort("FINAL STAGE CONVERSION ERROR");
161161
}
162162

163163
msg.status() << "Generating GOTO Program" << messaget::eom;
@@ -173,3 +173,22 @@ goto_modelt initialize_goto_model(
173173

174174
return goto_model;
175175
}
176+
177+
goto_model_initialization_errort::goto_model_initialization_errort(std::string message)
178+
: goto_model_initialization_errort(std::move(message), source_locationt())
179+
{}
180+
181+
std::string goto_model_initialization_errort::what() const noexcept {
182+
std::string what_msg = message;
183+
if(source_location.is_not_nil())
184+
{
185+
what_msg += "\nsource location: " + source_location.as_string();
186+
}
187+
return what_msg;
188+
}
189+
190+
goto_model_initialization_errort::goto_model_initialization_errort(std::string message, source_locationt source_location)
191+
: message(std::move(message)), source_location(std::move(source_location))
192+
{
193+
194+
}

src/goto-programs/initialize_goto_model.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ Author: Daniel Kroening, [email protected]
1616
#include <util/cmdline.h>
1717

1818
#include "goto_model.h"
19+
#include <string>
20+
#include <util/exception_utils.h>
21+
22+
class goto_model_initialization_errort : public cprover_exceptiont
23+
{
24+
public:
25+
explicit goto_model_initialization_errort(std::string message);
26+
goto_model_initialization_errort(std::string message, source_locationt source_location);
27+
std::string what() const noexcept override;
28+
private:
29+
std::string message;
30+
source_locationt source_location;
31+
};
1932

2033
goto_modelt initialize_goto_model(
2134
const cmdlinet &cmdline,

0 commit comments

Comments
 (0)