Skip to content

Commit 3f5847c

Browse files
authored
Merge pull request #2894 from diffblue/linker-get-symbols
linker: non-recursive version of get_symbols
2 parents 854f261 + 0a255fd commit 3f5847c

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

src/linking/remove_internal_symbols.cpp

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,44 @@ Author: Daniel Kroening
1919

2020
#include "static_lifetime_init.h"
2121

22-
void get_symbols_rec(
22+
static void get_symbols(
2323
const namespacet &ns,
2424
const symbolt &symbol,
2525
find_symbols_sett &dest)
2626
{
27-
dest.insert(symbol.name);
27+
std::vector<const symbolt *> working_set;
2828

29-
find_symbols_sett new_symbols;
29+
working_set.push_back(&symbol);
3030

31-
find_type_and_expr_symbols(symbol.type, new_symbols);
32-
find_type_and_expr_symbols(symbol.value, new_symbols);
33-
34-
if(symbol.type.id()==ID_code)
31+
while(!working_set.empty())
3532
{
36-
const code_typet &code_type=to_code_type(symbol.type);
37-
const code_typet::parameterst &parameters=code_type.parameters();
33+
const symbolt *s = working_set.back();
34+
working_set.pop_back();
35+
const symbolt &symbol = *s;
3836

39-
for(code_typet::parameterst::const_iterator
40-
it=parameters.begin();
41-
it!=parameters.end();
42-
it++)
43-
{
44-
irep_idt id=it->get_identifier();
45-
const symbolt *s;
46-
// identifiers for prototypes need not exist
47-
if(!ns.lookup(id, s))
48-
new_symbols.insert(id);
49-
}
50-
}
37+
if(!dest.insert(symbol.name).second)
38+
continue;
5139

52-
for(find_symbols_sett::const_iterator
53-
it=new_symbols.begin();
54-
it!=new_symbols.end();
55-
it++)
56-
{
57-
if(dest.find(*it)==dest.end())
40+
find_symbols_sett new_symbols;
41+
42+
find_type_and_expr_symbols(symbol.type, new_symbols);
43+
find_type_and_expr_symbols(symbol.value, new_symbols);
44+
45+
for(const auto &s : new_symbols)
46+
working_set.push_back(&ns.lookup(s));
47+
48+
if(symbol.type.id() == ID_code)
5849
{
59-
dest.insert(*it);
60-
get_symbols_rec(ns, ns.lookup(*it), dest); // recursive call
50+
const code_typet &code_type = to_code_type(symbol.type);
51+
const code_typet::parameterst &parameters = code_type.parameters();
52+
53+
for(const auto &p : parameters)
54+
{
55+
const symbolt *s;
56+
// identifiers for prototypes need not exist
57+
if(!ns.lookup(p.get_identifier(), s))
58+
working_set.push_back(s);
59+
}
6160
}
6261
}
6362
}
@@ -105,7 +104,7 @@ void remove_internal_symbols(
105104

106105
if(special.find(symbol.name)!=special.end())
107106
{
108-
get_symbols_rec(ns, symbol, exported);
107+
get_symbols(ns, symbol, exported);
109108
continue;
110109
}
111110

@@ -135,7 +134,7 @@ void remove_internal_symbols(
135134
// body? not local (i.e., "static")?
136135
if(has_body &&
137136
(!is_file_local || (config.main==symbol.name.c_str())))
138-
get_symbols_rec(ns, symbol, exported);
137+
get_symbols(ns, symbol, exported);
139138
}
140139
else
141140
{
@@ -144,7 +143,7 @@ void remove_internal_symbols(
144143
if((has_initializer || !symbol.is_extern) &&
145144
!is_file_local)
146145
{
147-
get_symbols_rec(ns, symbol, exported);
146+
get_symbols(ns, symbol, exported);
148147
}
149148
}
150149
}

0 commit comments

Comments
 (0)