Skip to content

Commit 6589d99

Browse files
authored
Merge pull request #4257 from romainbrenguier/feature/add-const
Define an add_const utility function
2 parents 3a577d7 + 32af11e commit 6589d99

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/goto-symex/goto_symex_state.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Author: Daniel Kroening, [email protected]
1414
#include <cstdlib>
1515
#include <iostream>
1616

17+
#include <util/as_const.h>
1718
#include <util/base_exceptions.h>
1819
#include <util/exception_utils.h>
1920
#include <util/expr_util.h>
@@ -315,7 +316,7 @@ void goto_symex_statet::rename(
315316
else if(expr.id()==ID_symbol)
316317
{
317318
// we never rename function symbols
318-
if(expr.type().id() == ID_code)
319+
if(as_const(expr).type().id() == ID_code)
319320
{
320321
rename(
321322
expr.type(),
@@ -333,7 +334,8 @@ void goto_symex_statet::rename(
333334
{
334335
auto &address_of_expr = to_address_of_expr(expr);
335336
rename_address(address_of_expr.object(), ns, level);
336-
to_pointer_type(expr.type()).subtype() = address_of_expr.object().type();
337+
to_pointer_type(expr.type()).subtype() =
338+
as_const(address_of_expr).object().type();
337339
}
338340
else
339341
{
@@ -343,12 +345,13 @@ void goto_symex_statet::rename(
343345
Forall_operands(it, expr)
344346
rename(*it, ns, level);
345347

348+
const exprt &c_expr = as_const(expr);
346349
INVARIANT(
347350
(expr.id() != ID_with ||
348-
expr.type() == to_with_expr(expr).old().type()) &&
351+
c_expr.type() == to_with_expr(c_expr).old().type()) &&
349352
(expr.id() != ID_if ||
350-
(expr.type() == to_if_expr(expr).true_case().type() &&
351-
expr.type() == to_if_expr(expr).false_case().type())),
353+
(c_expr.type() == to_if_expr(c_expr).true_case().type() &&
354+
c_expr.type() == to_if_expr(c_expr).false_case().type())),
352355
"Type of renamed expr should be the same as operands for with_exprt and "
353356
"if_exprt");
354357
}

src/util/as_const.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*******************************************************************\
2+
3+
Module: Util
4+
5+
Author: Romain Brenguier, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_AS_CONST_H
10+
#define CPROVER_UTIL_AS_CONST_H
11+
12+
/// Return a reference to the same object but ensures the type is const
13+
template <typename T>
14+
const T &as_const(T &value)
15+
{
16+
return static_cast<const T &>(value);
17+
}
18+
19+
#endif // CPROVER_UTIL_AS_CONST_H

0 commit comments

Comments
 (0)