Skip to content

Commit 139861f

Browse files
committed
Add variable collision check for function arguments
This adds variable name collision checking for collision between function arguments and local function variables. Fixes #6242
1 parent b0b71d0 commit 139861f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/ansi-c/expr2c.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,34 @@ void expr2ct::get_shorthands(const exprt &expr)
176176
has_collision=!ns_collision[func].insert(sh).second;
177177
}
178178

179+
if(!has_collision)
180+
{
181+
// We could also conflict with a function argument, the code below
182+
// finds the function we're in, and checks we don't conflict with
183+
// any argument to the function
184+
const std::string symbol_str = id2string(symbol_id);
185+
const std::string func = symbol_str.substr(0, symbol_str.find("::"));
186+
const symbolt *func_symbol;
187+
if(!ns.lookup(func, func_symbol))
188+
{
189+
if(can_cast_type<code_typet>(func_symbol->type))
190+
{
191+
const auto func_type =
192+
type_checked_cast<code_typet>(func_symbol->type);
193+
const auto params = func_type.parameters();
194+
for(const auto &param : params)
195+
{
196+
const auto param_id = param.get_identifier();
197+
if(param_id != symbol_id && sh == id_shorthand(param_id))
198+
{
199+
has_collision = true;
200+
break;
201+
}
202+
}
203+
}
204+
}
205+
}
206+
179207
if(has_collision)
180208
sh = clean_identifier(symbol_id);
181209

0 commit comments

Comments
 (0)