Skip to content

Commit d750d01

Browse files
authored
Merge pull request #8216 from tautschnig/cleanup/expr-with-source-location
Make exprt::with_source_location type safe
2 parents 47a62f6 + 9d582ab commit d750d01

File tree

5 files changed

+15
-23
lines changed

5 files changed

+15
-23
lines changed

src/goto-instrument/contracts/dynamic-frames/dfcc_wrapper_program.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ void dfcc_wrapper_programt::encode_ensures_clauses()
607607
{
608608
exprt ensures = to_lambda_expr(e)
609609
.application(contract_lambda_parameters)
610-
.with_source_location<exprt>(e);
610+
.with_source_location(e);
611611

612612
if(has_subexpr(ensures, ID_exists) || has_subexpr(ensures, ID_forall))
613613
add_quantified_variable(goto_model.symbol_table, ensures, language_mode);

src/goto-programs/builtin_functions.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,7 @@ void goto_convertt::do_function_call_symbol(
840840

841841
// use symbol->symbol_expr() to ensure we use the type from the symbol table
842842
code_function_callt function_call(
843-
lhs,
844-
symbol->symbol_expr().with_source_location<symbol_exprt>(function),
845-
arguments);
843+
lhs, symbol->symbol_expr().with_source_location(function), arguments);
846844
function_call.add_source_location() = function.source_location();
847845

848846
// remove void-typed assignments, which may have been created when the
@@ -1487,9 +1485,7 @@ void goto_convertt::do_function_call_symbol(
14871485
// insert function call
14881486
// use symbol->symbol_expr() to ensure we use the type from the symbol table
14891487
code_function_callt function_call(
1490-
lhs,
1491-
symbol->symbol_expr().with_source_location<symbol_exprt>(function),
1492-
arguments);
1488+
lhs, symbol->symbol_expr().with_source_location(function), arguments);
14931489
function_call.add_source_location()=function.source_location();
14941490

14951491
// remove void-typed assignments, which may have been created when the

src/goto-programs/rewrite_rw_ok.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static std::optional<exprt> rewrite_rw_ok(exprt expr, const namespacet &ns)
3232
r_or_w_ok->size(),
3333
ns.lookup(CPROVER_PREFIX "deallocated").symbol_expr(),
3434
ns.lookup(CPROVER_PREFIX "dead_object").symbol_expr()}
35-
.with_source_location<exprt>(*it);
35+
.with_source_location(*it);
3636

3737
it.mutate() = std::move(replacement);
3838
unchanged = false;
@@ -49,7 +49,7 @@ static std::optional<exprt> rewrite_rw_ok(exprt expr, const namespacet &ns)
4949
pointer_in_range->upper_bound(),
5050
ns.lookup(CPROVER_PREFIX "deallocated").symbol_expr(),
5151
ns.lookup(CPROVER_PREFIX "dead_object").symbol_expr()}
52-
.with_source_location<exprt>(*it);
52+
.with_source_location(*it);
5353

5454
it.mutate() = std::move(replacement);
5555
unchanged = false;

src/util/expr.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,19 @@ class exprt:public irept
9898
{ return (const operandst &)get_sub(); }
9999

100100
/// Add the source location from \p other, if it has any.
101-
template <typename T>
102-
T &with_source_location(const exprt &other) &
101+
exprt &with_source_location(const exprt &other) &
103102
{
104-
static_assert(
105-
std::is_base_of<exprt, T>::value,
106-
"The template argument T must be derived from exprt.");
107103
if(other.source_location().is_not_nil())
108104
add_source_location() = other.source_location();
109-
return *static_cast<T *>(this);
105+
return *this;
110106
}
111107

112108
/// Add the source location from \p other, if it has any.
113-
template <typename T>
114-
T &&with_source_location(const exprt &other) &&
109+
exprt &&with_source_location(const exprt &other) &&
115110
{
116-
static_assert(
117-
std::is_base_of<exprt, T>::value,
118-
"The template argument T must be derived from exprt.");
119111
if(other.source_location().is_not_nil())
120112
add_source_location() = other.source_location();
121-
return std::move(*static_cast<T *>(this));
113+
return std::move(*this);
122114
}
123115

124116
protected:

src/util/std_expr.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,12 +1600,16 @@ class array_exprt : public multi_ary_exprt
16001600

16011601
array_exprt &with_source_location(const exprt &other) &
16021602
{
1603-
return exprt::with_source_location<array_exprt>(other);
1603+
if(other.source_location().is_not_nil())
1604+
add_source_location() = other.source_location();
1605+
return *this;
16041606
}
16051607

16061608
array_exprt &&with_source_location(const exprt &other) &&
16071609
{
1608-
return std::move(*this).exprt::with_source_location<array_exprt>(other);
1610+
if(other.source_location().is_not_nil())
1611+
add_source_location() = other.source_location();
1612+
return std::move(*this);
16091613
}
16101614
};
16111615

0 commit comments

Comments
 (0)