Skip to content

Commit 6507d1e

Browse files
Use structured exceptions in initialize_goto_model
1 parent c89d376 commit 6507d1e

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
@@ -21,6 +23,7 @@ Author: Daniel Kroening, [email protected]
2123
#include <langapi/language.h>
2224

2325
#include <goto-programs/rebuild_goto_start_function.h>
26+
#include <util/exception_utils.h>
2427

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

4044
std::vector<std::string> binaries, sources;
@@ -66,21 +70,18 @@ goto_modelt initialize_goto_model(
6670

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

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

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

8687
languaget &language=*lf.language;
@@ -91,8 +92,8 @@ goto_modelt initialize_goto_model(
9192

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

9899
lf.get_modules();
@@ -102,17 +103,17 @@ goto_modelt initialize_goto_model(
102103

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

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

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

118119
bool binaries_provided_start=
@@ -149,14 +150,13 @@ goto_modelt initialize_goto_model(
149150

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

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

162162
msg.status() << "Generating GOTO Program" << messaget::eom;
@@ -172,3 +172,22 @@ goto_modelt initialize_goto_model(
172172

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

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)