Skip to content

Commit 7494ca0

Browse files
Merge pull request diffblue#217 from diffblue/cleanup/main_function_resultt
Tidy up main_function_resultt
2 parents af1756c + 55938f0 commit 7494ca0

File tree

4 files changed

+55
-72
lines changed

4 files changed

+55
-72
lines changed

cbmc/src/java_bytecode/ci_lazy_methods.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ bool ci_lazy_methodst::operator()(
6060
std::vector<irep_idt> method_worklist1;
6161
std::vector<irep_idt> method_worklist2;
6262

63-
auto main_function=
63+
main_function_resultt main_function=
6464
get_main_symbol(symbol_table, main_class, get_message_handler(), true);
65-
if(main_function.stop_convert)
65+
if(!main_function.is_success())
6666
{
6767
// Failed, mark all functions in the given main class(es)
6868
// reachable.

cbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ bool java_bytecode_languaget::generate_support_functions(
248248
{
249249
main_function_resultt res=
250250
get_main_symbol(symbol_table, main_class, get_message_handler());
251-
if(res.stop_convert)
252-
return res.error_found;
251+
if(!res.is_success())
252+
return res.is_error();
253253

254254
// generate the test harness in __CPROVER__start and a call the entry point
255255
return

cbmc/src/java_bytecode/java_entry_point.cpp

Lines changed: 35 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,6 @@ main_function_resultt get_main_symbol(
325325
message_handlert &message_handler,
326326
bool allow_no_body)
327327
{
328-
symbolt symbol;
329-
main_function_resultt res;
330-
331328
messaget message(message_handler);
332329

333330
// find main symbol
@@ -342,32 +339,26 @@ main_function_resultt get_main_symbol(
342339

343340
if(main_symbol_id==irep_idt())
344341
{
345-
message.error() << "main symbol resolution failed: "
346-
<< error_message << messaget::eom;
347-
res.main_function=symbol;
348-
res.error_found=true;
349-
res.stop_convert=true;
350-
return res;
342+
message.error()
343+
<< "main symbol resolution failed: " << error_message << messaget::eom;
344+
return main_function_resultt::Error;
351345
}
352346

353-
symbol_tablet::symbolst::const_iterator s_it=
354-
symbol_table.symbols.find(main_symbol_id);
347+
symbol_tablet::opt_const_symbol_reft symbol=
348+
symbol_table.lookup(main_symbol_id);
355349
INVARIANT(
356-
s_it!=symbol_table.symbols.end(),
350+
symbol,
357351
"resolve_friendly_method_name should return a symbol-table identifier");
358352

359-
symbol=s_it->second;
360-
361353
// check if it has a body
362-
if(symbol.value.is_nil() && !allow_no_body)
354+
if(symbol->get().value.is_nil() && !allow_no_body)
363355
{
364-
message.error() << "main method `" << main_class
365-
<< "' has no body" << messaget::eom;
366-
res.main_function=symbol;
367-
res.error_found=true;
368-
res.stop_convert=true;
369-
return res;
356+
message.error()
357+
<< "main method `" << main_class << "' has no body" << messaget::eom;
358+
return main_function_resultt::Error;
370359
}
360+
361+
return main_function_resultt(*symbol); // Return found function
371362
}
372363
else
373364
{
@@ -376,69 +367,49 @@ main_function_resultt get_main_symbol(
376367

377368
// are we given a main class?
378369
if(main_class.empty())
379-
{
380-
res.main_function=symbol;
381-
res.error_found=false;
382-
res.stop_convert=true;
383-
return res; // silently ignore
384-
}
370+
return main_function_resultt::NotFound; // silently ignore
385371

386-
std::string entry_method=
387-
id2string(main_class)+".main";
372+
std::string entry_method=id2string(main_class)+".main";
388373

389374
std::string prefix="java::"+entry_method+":";
390375

391376
// look it up
392-
std::set<irep_idt> matches;
377+
std::set<const symbolt *> matches;
393378

394-
for(symbol_tablet::symbolst::const_iterator
395-
s_it=symbol_table.symbols.begin();
396-
s_it!=symbol_table.symbols.end();
397-
s_it++)
379+
for(const auto &named_symbol : symbol_table.symbols)
398380
{
399-
if(s_it->second.type.id()==ID_code &&
400-
has_prefix(id2string(s_it->first), prefix))
401-
matches.insert(s_it->first);
381+
if(named_symbol.second.type.id()==ID_code
382+
&& has_prefix(id2string(named_symbol.first), prefix))
383+
{
384+
matches.insert(&named_symbol.second);
385+
}
402386
}
403387

404388
if(matches.empty())
405-
{
406389
// Not found, silently ignore
407-
res.main_function=symbol;
408-
res.error_found=false;
409-
res.stop_convert=true;
410-
return res;
411-
}
390+
return main_function_resultt::NotFound;
412391

413-
if(matches.size()>=2)
392+
if(matches.size()>1)
414393
{
415-
message.error() << "main method in `" << main_class
416-
<< "' is ambiguous" << messaget::eom;
417-
res.main_function=symbolt();
418-
res.error_found=true;
419-
res.stop_convert=true;
420-
return res; // give up with error, no main
394+
message.error()
395+
<< "main method in `" << main_class
396+
<< "' is ambiguous" << messaget::eom;
397+
return main_function_resultt::Error; // give up with error, no main
421398
}
422399

423400
// function symbol
424-
symbol=symbol_table.symbols.find(*matches.begin())->second;
401+
const symbolt &symbol=**matches.begin();
425402

426403
// check if it has a body
427404
if(symbol.value.is_nil() && !allow_no_body)
428405
{
429-
message.error() << "main method `" << main_class
430-
<< "' has no body" << messaget::eom;
431-
res.main_function=symbol;
432-
res.error_found=true;
433-
res.stop_convert=true;
434-
return res; // give up with error
406+
message.error()
407+
<< "main method `" << main_class << "' has no body" << messaget::eom;
408+
return main_function_resultt::Error; // give up with error
435409
}
436-
}
437410

438-
res.main_function=symbol;
439-
res.error_found=false;
440-
res.stop_convert=false;
441-
return res; // give up with error
411+
return symbol; // Return found function
412+
}
442413
}
443414

444415
/// Given the \p symbol_table and the \p main_class to test, this function
@@ -491,8 +462,8 @@ bool java_entry_point(
491462
messaget message(message_handler);
492463
main_function_resultt res=
493464
get_main_symbol(symbol_table, main_class, message_handler);
494-
if(res.stop_convert)
495-
return res.stop_convert;
465+
if(!res.is_success())
466+
return true;
496467
symbolt symbol=res.main_function;
497468

498469
assert(!symbol.value.is_nil());

cbmc/src/java_bytecode/java_entry_point.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,24 @@ bool java_entry_point(
2626
const object_factory_parameterst &object_factory_parameters,
2727
const select_pointer_typet &pointer_type_selector);
2828

29-
typedef struct
29+
struct main_function_resultt
3030
{
31+
enum statust { Success, Error, NotFound } status;
3132
symbolt main_function;
32-
bool error_found;
33-
bool stop_convert;
34-
} main_function_resultt;
33+
34+
main_function_resultt(statust status)
35+
: status(status)
36+
{
37+
}
38+
39+
main_function_resultt(const symbolt &main_function)
40+
: status(Success), main_function(main_function)
41+
{
42+
}
43+
44+
bool is_success() const { return status==Success; }
45+
bool is_error() const { return status==Error; }
46+
};
3547

3648
/// Figures out the entry point of the code to verify
3749
main_function_resultt get_main_symbol(

0 commit comments

Comments
 (0)