Skip to content

Commit dae3eca

Browse files
committed
Add INVARIANTs that goto declarations may not initialize
Initializations must be sepearated out of `code_declt` into a separate assignment before adding to goto program. This is the case already. These new INVARIANTs check that this continues to be true.
1 parent 0d0b964 commit dae3eca

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/goto-programs/goto_program.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,21 @@ class goto_programt
199199
const code_declt &get_decl() const
200200
{
201201
PRECONDITION(is_decl());
202-
return to_code_decl(code);
202+
const auto &decl = expr_checked_cast<code_declt>(code);
203+
INVARIANT(
204+
!decl.initial_value(),
205+
"code_declt in goto program may not contain initialization.");
206+
return decl;
203207
}
204208

205209
/// Set the declaration for DECL
206210
void set_decl(code_declt c)
207211
{
208212
PRECONDITION(is_decl());
213+
INVARIANT(
214+
!c.initial_value(),
215+
"Initialization must be separated from code_declt before adding to "
216+
"goto_instructiont.");
209217
code = std::move(c);
210218
}
211219

src/util/std_code.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ class code_declt:public codet
422422

423423
/// Returns the initial value to which the declared variable is initialized,
424424
/// or empty in the case where no initialisation is included.
425+
/// \note: Initial values may be present in the front end but they must be
426+
/// separated into a separate assignment when used in a `goto_instructiont`.
425427
optionalt<exprt> initial_value() const
426428
{
427429
if(operands().size() < 2)
@@ -431,6 +433,8 @@ class code_declt:public codet
431433

432434
/// Sets the value to which this declaration initializes the declared
433435
/// variable. Empty optional maybe passed to remove existing initialisation.
436+
/// \note: Initial values may be present in the front end but they must be
437+
/// separated into a separate assignment when used in a `goto_instructiont`.
434438
void set_initial_value(optionalt<exprt> initial_value)
435439
{
436440
if(!initial_value)

0 commit comments

Comments
 (0)