Skip to content

Commit f6f4cd3

Browse files
committed
Avoid extern/parameter name collisions in show-goto-functions/dump-c output
While the preceding patch in this series ensures that the internal representation is consistent, plain-text output would still have collisions between parameter names and globals declared extern, because parameters were never renamed. We now detect conflicts between prettified names and global variable names. Also added a comment explaining a somewhat obscure bit of code.
1 parent 9255272 commit f6f4cd3

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/ansi-c/expr2c.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void expr2ct::get_shorthands(const exprt &expr)
101101
find_symbols_sett symbols;
102102
find_symbols(expr, symbols);
103103

104-
// avoid renaming parameters
104+
// avoid renaming parameters, if possible
105105
for(find_symbols_sett::const_iterator
106106
it=symbols.begin();
107107
it!=symbols.end();
@@ -115,7 +115,16 @@ void expr2ct::get_shorthands(const exprt &expr)
115115

116116
irep_idt sh=id_shorthand(*it);
117117

118-
ns_collision[symbol->location.get_function()].insert(sh);
118+
std::string func = id2string(*it);
119+
func = func.substr(0, func.rfind("::"));
120+
121+
// if there is a global symbol of the same name as the shorthand (even if
122+
// not present in this particular expression) then there is a collision
123+
const symbolt *global_symbol;
124+
if(!ns.lookup(sh, global_symbol))
125+
sh = func + "$$" + id2string(sh);
126+
127+
ns_collision[func].insert(sh);
119128

120129
if(!shorthands.insert(std::make_pair(*it, sh)).second)
121130
UNREACHABLE;
@@ -137,6 +146,8 @@ void expr2ct::get_shorthands(const exprt &expr)
137146

138147
if(!has_collision)
139148
{
149+
// if there is a global symbol of the same name as the shorthand (even if
150+
// not present in this particular expression) then there is a collision
140151
const symbolt *symbol;
141152
has_collision=!ns.lookup(sh, symbol);
142153
}

0 commit comments

Comments
 (0)