Skip to content

Commit 861a411

Browse files
committed
Maintain source location while replacing symbol expressions
Linking (and also other workflows) use expression replacement. Doing so must not destroy the source location annotated to an expression. This left us with instructions without location.
1 parent 37d97f5 commit 861a411

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int foo()
2+
{
3+
return 0;
4+
}
5+
6+
int main()
7+
{
8+
int result;
9+
result = foo();
10+
return result;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int foo();
2+
3+
int bar()
4+
{
5+
return foo();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
other.c
4+
file main.c line 9 function main
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
--
9+
We previously lost the location attached to the call of `foo` in function main.

src/linking/linking.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,16 @@ bool casting_replace_symbolt::replace_symbol_expr(symbol_exprt &s) const
100100

101101
const exprt &e = it->second;
102102

103+
source_locationt previous_source_location{s.source_location()};
103104
if(e.type().id() != ID_array && e.type().id() != ID_code)
104105
{
105106
typet type = s.type();
106107
static_cast<exprt &>(s) = typecast_exprt::conditional_cast(e, type);
107108
}
108109
else
109110
static_cast<exprt &>(s) = e;
111+
if(previous_source_location.is_not_nil())
112+
s.add_source_location() = std::move(previous_source_location);
110113

111114
return false;
112115
}

src/util/replace_symbol.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ bool replace_symbolt::replace_symbol_expr(symbol_exprt &s) const
5151
s.type() == it->second.type(),
5252
"types to be replaced should match. s.type:\n" + s.type().pretty() +
5353
"\nit->second.type:\n" + it->second.type().pretty());
54+
source_locationt previous_source_location{s.source_location()};
5455
static_cast<exprt &>(s) = it->second;
56+
if(previous_source_location.is_not_nil())
57+
s.add_source_location() = std::move(previous_source_location);
5558

5659
return false;
5760
}
@@ -334,7 +337,10 @@ bool unchecked_replace_symbolt::replace_symbol_expr(symbol_exprt &s) const
334337
if(it == expr_map.end())
335338
return true;
336339

340+
source_locationt previous_source_location{s.source_location()};
337341
static_cast<exprt &>(s) = it->second;
342+
if(previous_source_location.is_not_nil())
343+
s.add_source_location() = std::move(previous_source_location);
338344

339345
return false;
340346
}
@@ -415,7 +421,10 @@ bool address_of_aware_replace_symbolt::replace_symbol_expr(
415421

416422
// Note s_copy is no longer a symbol_exprt due to the replace operation,
417423
// and after this line `s` won't be either
424+
source_locationt previous_source_location{s.source_location()};
418425
s = s_copy;
426+
if(previous_source_location.is_not_nil())
427+
s.add_source_location() = std::move(previous_source_location);
419428

420429
return false;
421430
}

0 commit comments

Comments
 (0)