Skip to content

Make exprt::with_source_location type safe #8216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ void dfcc_wrapper_programt::encode_ensures_clauses()
{
exprt ensures = to_lambda_expr(e)
.application(contract_lambda_parameters)
.with_source_location<exprt>(e);
.with_source_location(e);

if(has_subexpr(ensures, ID_exists) || has_subexpr(ensures, ID_forall))
add_quantified_variable(goto_model.symbol_table, ensures, language_mode);
Expand Down
8 changes: 2 additions & 6 deletions src/goto-programs/builtin_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,9 +840,7 @@ void goto_convertt::do_function_call_symbol(

// use symbol->symbol_expr() to ensure we use the type from the symbol table
code_function_callt function_call(
lhs,
symbol->symbol_expr().with_source_location<symbol_exprt>(function),
arguments);
lhs, symbol->symbol_expr().with_source_location(function), arguments);
function_call.add_source_location() = function.source_location();

// remove void-typed assignments, which may have been created when the
Expand Down Expand Up @@ -1487,9 +1485,7 @@ void goto_convertt::do_function_call_symbol(
// insert function call
// use symbol->symbol_expr() to ensure we use the type from the symbol table
code_function_callt function_call(
lhs,
symbol->symbol_expr().with_source_location<symbol_exprt>(function),
arguments);
lhs, symbol->symbol_expr().with_source_location(function), arguments);
function_call.add_source_location()=function.source_location();

// remove void-typed assignments, which may have been created when the
Expand Down
4 changes: 2 additions & 2 deletions src/goto-programs/rewrite_rw_ok.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static std::optional<exprt> rewrite_rw_ok(exprt expr, const namespacet &ns)
r_or_w_ok->size(),
ns.lookup(CPROVER_PREFIX "deallocated").symbol_expr(),
ns.lookup(CPROVER_PREFIX "dead_object").symbol_expr()}
.with_source_location<exprt>(*it);
.with_source_location(*it);

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

it.mutate() = std::move(replacement);
unchanged = false;
Expand Down
16 changes: 4 additions & 12 deletions src/util/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,19 @@ class exprt:public irept
{ return (const operandst &)get_sub(); }

/// Add the source location from \p other, if it has any.
template <typename T>
T &with_source_location(const exprt &other) &
exprt &with_source_location(const exprt &other) &
{
static_assert(
std::is_base_of<exprt, T>::value,
"The template argument T must be derived from exprt.");
if(other.source_location().is_not_nil())
add_source_location() = other.source_location();
return *static_cast<T *>(this);
return *this;
}

/// Add the source location from \p other, if it has any.
template <typename T>
T &&with_source_location(const exprt &other) &&
exprt &&with_source_location(const exprt &other) &&
{
static_assert(
std::is_base_of<exprt, T>::value,
"The template argument T must be derived from exprt.");
if(other.source_location().is_not_nil())
add_source_location() = other.source_location();
return std::move(*static_cast<T *>(this));
return std::move(*this);
}

protected:
Expand Down
8 changes: 6 additions & 2 deletions src/util/std_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1600,12 +1600,16 @@ class array_exprt : public multi_ary_exprt

array_exprt &with_source_location(const exprt &other) &
{
return exprt::with_source_location<array_exprt>(other);
if(other.source_location().is_not_nil())
add_source_location() = other.source_location();
return *this;
}

array_exprt &&with_source_location(const exprt &other) &&
{
return std::move(*this).exprt::with_source_location<array_exprt>(other);
if(other.source_location().is_not_nil())
add_source_location() = other.source_location();
return std::move(*this);
}
};

Expand Down