Skip to content

Commit 2b18e0c

Browse files
authored
Merge pull request #1562 from NathanJPhillips/feature/extend-main_function_result
Tidied up get_main_symbol
2 parents 89a1132 + 0aeb459 commit 2b18e0c

File tree

4 files changed

+68
-72
lines changed

4 files changed

+68
-72
lines changed

src/java_bytecode/ci_lazy_methods.cpp

+2-2
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.

src/java_bytecode/java_bytecode_language.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ bool java_bytecode_languaget::generate_support_functions(
264264

265265
main_function_resultt res=
266266
get_main_symbol(symbol_table, main_class, get_message_handler());
267-
if(res.stop_convert)
268-
return res.error_found;
267+
if(!res.is_success())
268+
return res.is_error();
269269

270270
// generate the test harness in __CPROVER__start and a call the entry point
271271
return

src/java_bytecode/java_entry_point.cpp

+34-64
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,6 @@ main_function_resultt get_main_symbol(
324324
message_handlert &message_handler,
325325
bool allow_no_body)
326326
{
327-
symbolt symbol;
328-
main_function_resultt res;
329-
330327
messaget message(message_handler);
331328

332329
// find main symbol
@@ -341,32 +338,25 @@ main_function_resultt get_main_symbol(
341338

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

352-
symbol_tablet::symbolst::const_iterator s_it=
353-
symbol_table.symbols.find(main_symbol_id);
346+
const symbolt *symbol = symbol_table.lookup(main_symbol_id);
354347
INVARIANT(
355-
s_it!=symbol_table.symbols.end(),
348+
symbol != nullptr,
356349
"resolve_friendly_method_name should return a symbol-table identifier");
357350

358-
symbol=s_it->second;
359-
360351
// check if it has a body
361-
if(symbol.value.is_nil() && !allow_no_body)
352+
if(symbol->value.is_nil() && !allow_no_body)
362353
{
363-
message.error() << "main method `" << main_class
364-
<< "' has no body" << messaget::eom;
365-
res.main_function=symbol;
366-
res.error_found=true;
367-
res.stop_convert=true;
368-
return res;
354+
message.error()
355+
<< "main method `" << main_class << "' has no body" << messaget::eom;
356+
return main_function_resultt::Error;
369357
}
358+
359+
return *symbol; // Return found function
370360
}
371361
else
372362
{
@@ -375,69 +365,49 @@ main_function_resultt get_main_symbol(
375365

376366
// are we given a main class?
377367
if(main_class.empty())
378-
{
379-
res.main_function=symbol;
380-
res.error_found=false;
381-
res.stop_convert=true;
382-
return res; // silently ignore
383-
}
368+
return main_function_resultt::NotFound; // silently ignore
384369

385-
std::string entry_method=
386-
id2string(main_class)+".main";
370+
std::string entry_method = id2string(main_class) + ".main";
387371

388372
std::string prefix="java::"+entry_method+":";
389373

390374
// look it up
391-
std::set<irep_idt> matches;
375+
std::set<const symbolt *> matches;
392376

393-
for(symbol_tablet::symbolst::const_iterator
394-
s_it=symbol_table.symbols.begin();
395-
s_it!=symbol_table.symbols.end();
396-
s_it++)
377+
for(const auto &named_symbol : symbol_table.symbols)
397378
{
398-
if(s_it->second.type.id()==ID_code &&
399-
has_prefix(id2string(s_it->first), prefix))
400-
matches.insert(s_it->first);
379+
if(named_symbol.second.type.id() == ID_code
380+
&& has_prefix(id2string(named_symbol.first), prefix))
381+
{
382+
matches.insert(&named_symbol.second);
383+
}
401384
}
402385

403386
if(matches.empty())
404-
{
405387
// Not found, silently ignore
406-
res.main_function=symbol;
407-
res.error_found=false;
408-
res.stop_convert=true;
409-
return res;
410-
}
388+
return main_function_resultt::NotFound;
411389

412-
if(matches.size()>=2)
390+
if(matches.size() > 1)
413391
{
414-
message.error() << "main method in `" << main_class
415-
<< "' is ambiguous" << messaget::eom;
416-
res.main_function=symbolt();
417-
res.error_found=true;
418-
res.stop_convert=true;
419-
return res; // give up with error, no main
392+
message.error()
393+
<< "main method in `" << main_class
394+
<< "' is ambiguous" << messaget::eom;
395+
return main_function_resultt::Error; // give up with error, no main
420396
}
421397

422398
// function symbol
423-
symbol=symbol_table.symbols.find(*matches.begin())->second;
399+
const symbolt &symbol = **matches.begin();
424400

425401
// check if it has a body
426402
if(symbol.value.is_nil() && !allow_no_body)
427403
{
428-
message.error() << "main method `" << main_class
429-
<< "' has no body" << messaget::eom;
430-
res.main_function=symbol;
431-
res.error_found=true;
432-
res.stop_convert=true;
433-
return res; // give up with error
404+
message.error()
405+
<< "main method `" << main_class << "' has no body" << messaget::eom;
406+
return main_function_resultt::Error; // give up with error
434407
}
435-
}
436408

437-
res.main_function=symbol;
438-
res.error_found=false;
439-
res.stop_convert=false;
440-
return res; // give up with error
409+
return symbol; // Return found function
410+
}
441411
}
442412

443413
/// Given the \p symbol_table and the \p main_class to test, this function
@@ -490,8 +460,8 @@ bool java_entry_point(
490460
messaget message(message_handler);
491461
main_function_resultt res=
492462
get_main_symbol(symbol_table, main_class, message_handler);
493-
if(res.stop_convert)
494-
return res.stop_convert;
463+
if(!res.is_success())
464+
return true;
495465
symbolt symbol=res.main_function;
496466

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

src/java_bytecode/java_entry_point.h

+30-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,38 @@ 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
32+
{
33+
Success,
34+
Error,
35+
NotFound
36+
} status;
3137
symbolt main_function;
32-
bool error_found;
33-
bool stop_convert;
34-
} main_function_resultt;
38+
39+
// Implicit conversion doesn't lose information and allows return of status
40+
// NOLINTNEXTLINE(runtime/explicit)
41+
main_function_resultt(statust status) : status(status)
42+
{
43+
}
44+
45+
// Implicit conversion doesn't lose information and allows return of symbol
46+
// NOLINTNEXTLINE(runtime/explicit)
47+
main_function_resultt(const symbolt &main_function)
48+
: status(Success), main_function(main_function)
49+
{
50+
}
51+
52+
bool is_success() const
53+
{
54+
return status == Success;
55+
}
56+
bool is_error() const
57+
{
58+
return status == Error;
59+
}
60+
};
3561

3662
/// Figures out the entry point of the code to verify
3763
main_function_resultt get_main_symbol(

0 commit comments

Comments
 (0)