@@ -48,15 +48,45 @@ static bool link_functions(
4848 goto_functionst &dest_functions,
4949 const symbol_tablet &src_symbol_table,
5050 goto_functionst &src_functions,
51- const rename_symbolt &rename_symbol,
51+ rename_symbolt &rename_symbol,
5252 const std::unordered_set<irep_idt> &weak_symbols)
5353{
5454 namespacet ns (dest_symbol_table);
5555 namespacet src_ns (src_symbol_table);
5656
57+ // remove any unnecessary duplicates
58+ std::unordered_set<irep_idt> duplicates;
59+ for (const auto &f_pair : src_functions.function_map )
60+ {
61+ rename_symbolt::expr_mapt::iterator rename_it =
62+ rename_symbol.expr_map .find (f_pair.first );
63+ if (rename_it == rename_symbol.expr_map .end ())
64+ continue ;
65+
66+ std::string new_name = id2string (rename_it->second );
67+ std::string::size_type suffix_pos = new_name.find (" $link" );
68+ if (suffix_pos == std::string::npos)
69+ continue ;
70+
71+ irep_idt original_name = new_name.substr (0 , suffix_pos);
72+ goto_functionst::function_mapt::iterator existing_func =
73+ dest_functions.function_map .find (original_name);
74+ if (existing_func == dest_functions.function_map .end ())
75+ continue ;
76+
77+ if (f_pair.second .body .equals (existing_func->second .body ))
78+ {
79+ duplicates.insert (f_pair.first );
80+ rename_symbol.expr_map .erase (rename_it);
81+ }
82+ }
83+
5784 // merge functions
5885 for (auto &gf_entry : src_functions.function_map )
5986 {
87+ if (duplicates.find (gf_entry.first ) != duplicates.end ())
88+ continue ;
89+
6090 // the function might have been renamed
6191 rename_symbolt::expr_mapt::const_iterator e_it =
6292 rename_symbol.expr_map .find (gf_entry.first );
0 commit comments