Skip to content

Commit 90b1ed6

Browse files
author
Daniel Kroening
authored
Merge pull request #1281 from tautschnig/interpreter-assert-cleanup
Interpreter: replace assert by invariants
2 parents b95359f + fd6f91c commit 90b1ed6

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

src/goto-programs/interpreter.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include <algorithm>
1919
#include <string.h>
2020

21+
#include <util/invariant.h>
2122
#include <util/std_types.h>
2223
#include <util/symbol_table.h>
2324
#include <util/ieee_float.h>
@@ -380,7 +381,9 @@ void interpretert::execute_other()
380381
const irep_idt &statement=pc->code.get_statement();
381382
if(statement==ID_expression)
382383
{
383-
assert(pc->code.operands().size()==1);
384+
DATA_INVARIANT(
385+
pc->code.operands().size()==1,
386+
"expression statement expected to have one operand");
384387
mp_vectort rhs;
385388
evaluate(pc->code.op0(), rhs);
386389
}
@@ -409,7 +412,7 @@ void interpretert::execute_other()
409412

410413
void interpretert::execute_decl()
411414
{
412-
assert(pc->code.get_statement()==ID_decl);
415+
PRECONDITION(pc->code.get_statement()==ID_decl);
413416
}
414417

415418
/// retrieves the member at offset
@@ -515,7 +518,7 @@ exprt interpretert::get_value(
515518
std::size_t offset)
516519
{
517520
const typet real_type=ns.follow(type);
518-
assert(!rhs.empty());
521+
PRECONDITION(!rhs.empty());
519522

520523
if(real_type.id()==ID_struct)
521524
{
@@ -807,7 +810,6 @@ void interpretert::execute_function_call()
807810
const code_typet::parametert &a=parameters[i];
808811
exprt symbol_expr(ID_symbol, a.type());
809812
symbol_expr.set(ID_identifier, a.get_identifier());
810-
assert(i<argument_values.size());
811813
assign(evaluate_address(symbol_expr), argument_values[i]);
812814
}
813815

@@ -822,8 +824,8 @@ void interpretert::execute_function_call()
822824
if(it!=function_input_vars.end())
823825
{
824826
mp_vectort value;
825-
assert(!it->second.empty());
826-
assert(!it->second.front().return_assignments.empty());
827+
PRECONDITION(!it->second.empty());
828+
PRECONDITION(!it->second.front().return_assignments.empty());
827829
evaluate(it->second.front().return_assignments.back().value, value);
828830
if(return_value_address>0)
829831
{
@@ -1013,7 +1015,8 @@ size_t interpretert::get_size(const typet &type)
10131015
// overflow behaviour.
10141016
exprt size_const=from_integer(i[0], size_expr.type());
10151017
mp_integer size_mp;
1016-
assert(!to_integer(size_const, size_mp));
1018+
bool ret=to_integer(size_const, size_mp);
1019+
CHECK_RETURN(!ret);
10171020
return subtype_size*integer2unsigned(size_mp);
10181021
}
10191022
return subtype_size;

src/goto-programs/interpreter_class.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <stack>
1616

1717
#include <util/arith_tools.h>
18+
#include <util/invariant.h>
1819
#include <util/sparse_vector.h>
1920

2021
#include "goto_functions.h"
@@ -113,7 +114,7 @@ class interpretert:public messaget
113114
auto lower_bound=inverse_memory_map.lower_bound(address);
114115
if(lower_bound->first!=address)
115116
{
116-
assert(lower_bound!=inverse_memory_map.begin());
117+
CHECK_RETURN(lower_bound!=inverse_memory_map.begin());
117118
--lower_bound;
118119
}
119120
return *lower_bound;
@@ -131,7 +132,7 @@ class interpretert:public messaget
131132

132133
std::size_t base_address_to_alloc_size(std::size_t address) const
133134
{
134-
assert(address_to_offset(address)==0);
135+
PRECONDITION(address_to_offset(address)==0);
135136
auto upper_bound=inverse_memory_map.upper_bound(address);
136137
std::size_t next_alloc_address=
137138
upper_bound==inverse_memory_map.end() ?

src/goto-programs/interpreter_evaluate.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "interpreter_class.h"
1313

14-
#include <cassert>
1514
#include <iostream>
1615
#include <sstream>
1716

1817
#include <util/ieee_float.h>
18+
#include <util/invariant.h>
1919
#include <util/fixedbv.h>
2020
#include <util/std_expr.h>
2121
#include <util/pointer_offset_size.h>
@@ -911,7 +911,7 @@ void interpretert::evaluate(
911911
if(expr.op0().id()==ID_array)
912912
{
913913
const auto &ops=expr.op0().operands();
914-
assert(read_from_index.is_long());
914+
DATA_INVARIANT(read_from_index.is_long(), "index is too large");
915915
if(read_from_index>=0 && read_from_index<ops.size())
916916
{
917917
evaluate(ops[read_from_index.to_long()], dest);
@@ -924,12 +924,14 @@ void interpretert::evaluate(
924924
// This sort of construct comes from boolbv_get, but doesn't seem
925925
// to have an exprt yet. Its operands are a list of key-value pairs.
926926
const auto &ops=expr.op0().operands();
927-
assert(ops.size()%2==0);
927+
DATA_INVARIANT(
928+
ops.size()%2==0,
929+
"array-list has odd number of operands");
928930
for(size_t listidx=0; listidx!=ops.size(); listidx+=2)
929931
{
930932
mp_vectort elem_idx;
931933
evaluate(ops[listidx], elem_idx);
932-
assert(elem_idx.size()==1);
934+
CHECK_RETURN(elem_idx.size()==1);
933935
if(elem_idx[0]==read_from_index)
934936
{
935937
evaluate(ops[listidx+1], dest);
@@ -1177,7 +1179,7 @@ mp_integer interpretert::evaluate_address(
11771179
if(expr.operands().size()!=1)
11781180
throw "typecast expects one operand";
11791181

1180-
assert(expr.type().id()==ID_pointer);
1182+
PRECONDITION(expr.type().id()==ID_pointer);
11811183

11821184
return evaluate_address(expr.op0(), fail_quietly);
11831185
}

src/util/sparse_vector.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ Author: Romain Brenguier
1212
#ifndef CPROVER_UTIL_SPARSE_VECTOR_H
1313
#define CPROVER_UTIL_SPARSE_VECTOR_H
1414

15+
#include "invariant.h"
16+
1517
#include <cstdint>
1618
#include <map>
17-
#include <assert.h>
1819

1920
template<class T> class sparse_vectort
2021
{
@@ -29,13 +30,13 @@ template<class T> class sparse_vectort
2930

3031
const T &operator[](uint64_t idx) const
3132
{
32-
assert(idx<_size && "index out of range");
33+
INVARIANT(idx<_size, "index out of range");
3334
return underlying[idx];
3435
}
3536

3637
T &operator[](uint64_t idx)
3738
{
38-
assert(idx<_size && "index out of range");
39+
INVARIANT(idx<_size, "index out of range");
3940
return underlying[idx];
4041
}
4142

@@ -46,7 +47,7 @@ template<class T> class sparse_vectort
4647

4748
void resize(uint64_t new_size)
4849
{
49-
assert(new_size>=_size && "sparse vector can't be shrunk (yet)");
50+
INVARIANT(new_size>=_size, "sparse vector can't be shrunk (yet)");
5051
_size=new_size;
5152
}
5253

0 commit comments

Comments
 (0)