-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathget_module.cpp
124 lines (90 loc) · 2.91 KB
/
get_module.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*******************************************************************\
Module: Find module symbol using name
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Find module symbol using name
#include "get_module.h"
#include "message.h"
#include "range.h"
#include "symbol_table_base.h"
#include <list>
#include <set>
typedef std::list<const symbolt *> symbolptr_listt;
const symbolt &get_module_by_name(
const symbol_table_baset &symbol_table,
const std::string &module,
message_handlert &message_handler)
{
symbolptr_listt symbolptr_list;
messaget message(message_handler);
for(const auto &symbol_name_entry :
equal_range(symbol_table.symbol_base_map, module))
{
symbol_table_baset::symbolst::const_iterator it2 =
symbol_table.symbols.find(symbol_name_entry.second);
if(it2==symbol_table.symbols.end())
continue;
const symbolt &s=it2->second;
if(s.is_type || s.type.id()!=ID_module)
continue;
symbolptr_list.push_back(&s);
}
if(symbolptr_list.empty())
{
message.error() << "module '" << module << "' not found" << messaget::eom;
throw 0;
}
else if(symbolptr_list.size()>=2)
{
message.error() << "module '" << module << "' does not uniquely resolve:\n";
for(const symbolt *symbol_ptr : symbolptr_list)
message.error() << " " << symbol_ptr->name << '\n';
message.error() << messaget::eom;
throw 0;
}
// symbolptr_list has exactly one element
return *symbolptr_list.front();
}
const symbolt &get_module(
const symbol_table_baset &symbol_table,
const std::string &module,
message_handlert &message_handler)
{
if(!module.empty())
return get_module_by_name(symbol_table, module, message_handler);
symbolptr_listt symbolptr_list, main_symbolptr_list;
messaget message(message_handler);
for(const auto &symbol_pair : symbol_table.symbols)
{
const symbolt &s = symbol_pair.second;
if(s.type.id()!=ID_module)
continue;
// this is our default
if(s.base_name==ID_main)
return get_module_by_name(symbol_table, "main", message_handler);
symbolptr_list.push_back(&s);
}
if(symbolptr_list.empty())
{
message.error() << "no module found" << messaget::eom;
throw 0;
}
else if(symbolptr_list.size()>=2)
{
// sorted alphabetically
std::set<std::string> modules;
for(const symbolt *symbol_ptr : symbolptr_list)
modules.insert(id2string(symbol_ptr->pretty_name));
message.error() << "multiple modules found, please select one:\n";
for(const auto &s_it : modules)
message.error() << " " << s_it << '\n';
message.error() << messaget::eom;
throw 0;
}
// symbolptr_list has exactly one element
const symbolt &symbol=*symbolptr_list.front();
message.status() << "Using module '" << symbol.pretty_name << "'"
<< messaget::eom;
return symbol;
}