Skip to content

Commit fa58084

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 2caff5f commit fa58084

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
@@ -97,7 +97,7 @@ void expr2ct::get_shorthands(const exprt &expr)
9797
find_symbols_sett symbols;
9898
find_symbols(expr, symbols);
9999

100-
// avoid renaming parameters
100+
// avoid renaming parameters, if possible
101101
for(find_symbols_sett::const_iterator
102102
it=symbols.begin();
103103
it!=symbols.end();
@@ -111,7 +111,16 @@ void expr2ct::get_shorthands(const exprt &expr)
111111

112112
irep_idt sh=id_shorthand(*it);
113113

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

116125
if(!shorthands.insert(std::make_pair(*it, sh)).second)
117126
UNREACHABLE;
@@ -133,6 +142,8 @@ void expr2ct::get_shorthands(const exprt &expr)
133142

134143
if(!has_collision)
135144
{
145+
// if there is a global symbol of the same name as the shorthand (even if
146+
// not present in this particular expression) then there is a collision
136147
const symbolt *symbol;
137148
has_collision=!ns.lookup(sh, symbol);
138149
}

0 commit comments

Comments
 (0)