Skip to content

Commit e276b27

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 87c5948 commit e276b27

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
@@ -89,7 +89,7 @@ void expr2ct::get_shorthands(const exprt &expr)
8989
find_symbols_sett symbols;
9090
find_symbols(expr, symbols);
9191

92-
// avoid renaming parameters
92+
// avoid renaming parameters, if possible
9393
for(find_symbols_sett::const_iterator
9494
it=symbols.begin();
9595
it!=symbols.end();
@@ -103,7 +103,16 @@ void expr2ct::get_shorthands(const exprt &expr)
103103

104104
irep_idt sh=id_shorthand(*it);
105105

106-
ns_collision[symbol->location.get_function()].insert(sh);
106+
std::string func = id2string(*it);
107+
func = func.substr(0, func.rfind("::"));
108+
109+
// if there is a global symbol of the same name as the shorthand (even if
110+
// not present in this particular expression) then there is a collision
111+
const symbolt *global_symbol;
112+
if(!ns.lookup(sh, global_symbol))
113+
sh = func + "$$" + id2string(sh);
114+
115+
ns_collision[func].insert(sh);
107116

108117
if(!shorthands.insert(std::make_pair(*it, sh)).second)
109118
UNREACHABLE;
@@ -125,6 +134,8 @@ void expr2ct::get_shorthands(const exprt &expr)
125134

126135
if(!has_collision)
127136
{
137+
// if there is a global symbol of the same name as the shorthand (even if
138+
// not present in this particular expression) then there is a collision
128139
const symbolt *symbol;
129140
has_collision=!ns.lookup(sh, symbol);
130141
}

0 commit comments

Comments
 (0)