Skip to content

Commit f42cc33

Browse files
Addressed reviewers comments
1 parent 229817d commit f42cc33

File tree

3 files changed

+71
-79
lines changed

3 files changed

+71
-79
lines changed

src/analyses/uncaught_exceptions_analysis.cpp

+29-31
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ Module: Over-approximating uncaught exceptions analysis
55
Author: Cristina David
66
77
\*******************************************************************/
8+
89
#ifdef DEBUG
910
#include <iostream>
1011
#endif
1112
#include "uncaught_exceptions_analysis.h"
1213

1314
/*******************************************************************\
1415
15-
Function: get_static_type
16+
Function: get_exception_type
1617
1718
Inputs:
1819
@@ -22,15 +23,13 @@ Function: get_static_type
2223
2324
\*******************************************************************/
2425

25-
irep_idt uncaught_exceptions_domaint::get_static_type(const typet &type)
26+
irep_idt uncaught_exceptions_domaint::get_exception_type(const typet &type)
2627
{
27-
if(type.id()==ID_pointer)
28-
{
29-
return get_static_type(type.subtype());
30-
}
31-
else if(type.id()==ID_symbol)
28+
assert(type.id()==ID_pointer);
29+
30+
if(type.subtype().id()==ID_symbol)
3231
{
33-
return to_symbol_type(type).get_identifier();
32+
return to_symbol_type(type.subtype()).get_identifier();
3433
}
3534
return ID_empty;
3635
}
@@ -63,7 +62,7 @@ Function: uncaught_exceptions_domaint::join
6362
6463
Outputs:
6564
66-
Purpose:
65+
Purpose: The join operator for the uncaught exceptions domain
6766
6867
\*******************************************************************/
6968

@@ -94,7 +93,7 @@ Function: uncaught_exceptions_domaint::transform
9493
9594
Outputs:
9695
97-
Purpose:
96+
Purpose: The transformer for the uncaught exceptions domain
9897
9998
\*******************************************************************/
10099

@@ -111,7 +110,7 @@ void uncaught_exceptions_domaint::transform(
111110
{
112111
const exprt &exc_symbol=get_exception_symbol(instruction.code);
113112
// retrieve the static type of the thrown exception
114-
const irep_idt &type_id=get_static_type(exc_symbol.type());
113+
const irep_idt &type_id=get_exception_type(exc_symbol.type());
115114
bool assertion_error=
116115
id2string(type_id).find("java.lang.AssertionError")!=std::string::npos;
117116
if(!assertion_error)
@@ -153,12 +152,7 @@ void uncaught_exceptions_domaint::transform(
153152
join(caught);
154153
// remove the caught exceptions
155154
for(const auto &exc_id : caught)
156-
{
157-
const std::set<irep_idt>::iterator it=
158-
std::find(thrown.begin(), thrown.end(), exc_id);
159-
if(it!=thrown.end())
160-
thrown.erase(it);
161-
}
155+
thrown.erase(exc_id);
162156
stack_caught.pop_back();
163157
}
164158
}
@@ -189,14 +183,13 @@ Function: uncaught_exceptions_domaint::get_elements
189183
190184
Outputs:
191185
192-
Purpose:
186+
Purpose: Returns the value of the private member thrown
193187
194188
\*******************************************************************/
195189

196-
void uncaught_exceptions_domaint::get_elements(
197-
std::set<irep_idt> &elements)
190+
const std::set<irep_idt> &uncaught_exceptions_domaint::get_elements() const
198191
{
199-
elements=thrown;
192+
return thrown;
200193
}
201194

202195
/*******************************************************************\
@@ -207,7 +200,7 @@ Function: uncaught_exceptions_domaint::operator()
207200
208201
Outputs:
209202
210-
Purpose:
203+
Purpose: Constructs the class hierarchy
211204
212205
\*******************************************************************/
213206

@@ -225,7 +218,8 @@ Function: uncaught_exceptions_analysist::collect_uncaught_exceptions
225218
226219
Outputs:
227220
228-
Purpose:
221+
Purpose: Runs the uncaught exceptions analysis, which
222+
populates the exceptions map
229223
230224
\*******************************************************************/
231225

@@ -252,11 +246,12 @@ void uncaught_exceptions_analysist::collect_uncaught_exceptions(
252246
domain.transform(instr_it, *this, ns);
253247
}
254248
// did our estimation for the current function improve?
255-
std::set<irep_idt> elements;
256-
domain.get_elements(elements);
257-
change=change||
258-
exceptions_map[current_function->first].size()!=elements.size();
259-
exceptions_map[current_function->first]=elements;
249+
const std::set<irep_idt> &elements=domain.get_elements();
250+
if(exceptions_map[current_function->first].size()<elements.size())
251+
{
252+
change=true;
253+
exceptions_map[current_function->first]=elements;
254+
}
260255
}
261256
}
262257
}
@@ -269,7 +264,8 @@ Function: uncaught_exceptions_analysist::output
269264
270265
Outputs:
271266
272-
Purpose:
267+
Purpose: Prints the exceptions map that maps each method to the
268+
set of exceptions that may escape it
273269
274270
\*******************************************************************/
275271

@@ -300,7 +296,8 @@ Function: uncaught_exceptions_analysist::operator()
300296
301297
Outputs:
302298
303-
Purpose:
299+
Purpose: Applies the uncaught exceptions analysis and
300+
outputs the result
304301
305302
\*******************************************************************/
306303

@@ -323,7 +320,8 @@ Function: uncaught_exceptions
323320
324321
Outputs:
325322
326-
Purpose:
323+
Purpose: Applies the uncaught exceptions analysis and
324+
outputs the result
327325
328326
\*******************************************************************/
329327

src/analyses/uncaught_exceptions_analysis.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ class uncaught_exceptions_domaint
4343
stack_caught.clear();
4444
}
4545

46-
static irep_idt get_static_type(const typet &type);
46+
static irep_idt get_exception_type(const typet &type);
4747

4848
static exprt get_exception_symbol(const exprt &exor);
4949

50-
void get_elements(std::set<irep_idt> &elements);
50+
const std::set<irep_idt> &get_elements() const;
5151

5252
void operator()(const namespacet &ns);
5353

src/goto-programs/remove_exceptions.cpp

+40-46
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,9 @@ void remove_exceptionst::add_exceptional_returns(
117117

118118
// We generate an exceptional return value for any function that
119119
// contains a throw or a function call that may escape exceptions.
120-
bool add_exceptional_var=false;
121-
bool has_uncaught_exceptions=false;
122120
forall_goto_program_instructions(instr_it, goto_program)
123121
{
122+
bool has_uncaught_exceptions=false;
124123
if(instr_it->is_function_call())
125124
{
126125
const exprt &function_expr=
@@ -131,53 +130,53 @@ void remove_exceptionst::add_exceptional_returns(
131130
has_uncaught_exceptions=!exceptions_map[function_name].empty();
132131
}
133132

134-
const exprt &exc =
135-
uncaught_exceptions_domaint::get_exception_symbol(instr_it->code);
136-
bool assertion_error =
137-
id2string(uncaught_exceptions_domaint::get_static_type(exc.type())).
138-
find("java.lang.AssertionError")!=std::string::npos;
133+
bool assertion_error=false;
134+
if(instr_it->is_throw())
135+
{
136+
const exprt &exc =
137+
uncaught_exceptions_domaint::get_exception_symbol(instr_it->code);
138+
assertion_error =
139+
id2string(uncaught_exceptions_domaint::get_exception_type(exc.type())).
140+
find("java.lang.AssertionError")!=std::string::npos;
141+
}
139142

140-
// if we find a throw that is not AssertionError of a function call
143+
// if we find a throw different from AssertionError or a function call
141144
// that may escape exceptions, then we add an exceptional return
142145
// variable
143146
if((instr_it->is_throw() && !assertion_error)
144147
|| has_uncaught_exceptions)
145148
{
146-
add_exceptional_var=true;
149+
// look up the function symbol
150+
symbol_tablet::symbolst::iterator s_it=
151+
symbol_table.symbols.find(function_id);
152+
153+
assert(s_it!=symbol_table.symbols.end());
154+
155+
auxiliary_symbolt new_symbol;
156+
new_symbol.is_static_lifetime=true;
157+
new_symbol.module=function_symbol.module;
158+
new_symbol.base_name=id2string(function_symbol.base_name)+EXC_SUFFIX;
159+
new_symbol.name=id2string(function_symbol.name)+EXC_SUFFIX;
160+
new_symbol.mode=function_symbol.mode;
161+
new_symbol.type=typet(ID_pointer, empty_typet());
162+
symbol_table.add(new_symbol);
163+
164+
// initialize the exceptional return with NULL
165+
symbol_exprt lhs_expr_null=new_symbol.symbol_expr();
166+
null_pointer_exprt rhs_expr_null((pointer_typet(empty_typet())));
167+
goto_programt::targett t_null=
168+
goto_program.insert_before(goto_program.instructions.begin());
169+
t_null->make_assignment();
170+
t_null->source_location=
171+
goto_program.instructions.begin()->source_location;
172+
t_null->code=code_assignt(
173+
lhs_expr_null,
174+
rhs_expr_null);
175+
t_null->function=function_id;
176+
147177
break;
148178
}
149179
}
150-
151-
if(add_exceptional_var)
152-
{
153-
// look up the function symbol
154-
symbol_tablet::symbolst::iterator s_it=
155-
symbol_table.symbols.find(function_id);
156-
157-
assert(s_it!=symbol_table.symbols.end());
158-
159-
auxiliary_symbolt new_symbol;
160-
new_symbol.is_static_lifetime=true;
161-
new_symbol.module=function_symbol.module;
162-
new_symbol.base_name=id2string(function_symbol.base_name)+EXC_SUFFIX;
163-
new_symbol.name=id2string(function_symbol.name)+EXC_SUFFIX;
164-
new_symbol.mode=function_symbol.mode;
165-
new_symbol.type=typet(ID_pointer, empty_typet());
166-
symbol_table.add(new_symbol);
167-
168-
// initialize the exceptional return with NULL
169-
symbol_exprt lhs_expr_null=new_symbol.symbol_expr();
170-
null_pointer_exprt rhs_expr_null((pointer_typet(empty_typet())));
171-
goto_programt::targett t_null=
172-
goto_program.insert_before(goto_program.instructions.begin());
173-
t_null->make_assignment();
174-
t_null->source_location=
175-
goto_program.instructions.begin()->source_location;
176-
t_null->code=code_assignt(
177-
lhs_expr_null,
178-
rhs_expr_null);
179-
t_null->function=function_id;
180-
}
181180
}
182181

183182
/*******************************************************************\
@@ -293,7 +292,7 @@ void remove_exceptionst::instrument_throw(
293292
const exprt &exc_expr=
294293
uncaught_exceptions_domaint::get_exception_symbol(instr_it->code);
295294
bool assertion_error=id2string(
296-
uncaught_exceptions_domaint::get_static_type(exc_expr.type())).
295+
uncaught_exceptions_domaint::get_exception_type(exc_expr.type())).
297296
find("java.lang.AssertionError")!=std::string::npos;
298297

299298
// we don't count AssertionError (we couldn't catch it anyway
@@ -360,11 +359,6 @@ void remove_exceptionst::instrument_throw(
360359
t_dead->function=instr_it->function;
361360
}
362361

363-
// replace "throw x;" by "f#exception_value=x;"
364-
// exprt exc_expr=instr_it->code;
365-
// while(exc_expr.id()!=ID_symbol && exc_expr.has_operands())
366-
// exc_expr=exc_expr.op0();
367-
368362
// add the assignment with the appropriate cast
369363
code_assignt assignment(typecast_exprt(exc_thrown, exc_expr.type()),
370364
exc_expr);

0 commit comments

Comments
 (0)