Skip to content

Commit 8934bb1

Browse files
committed
Replace assert(...) by macros from invariant.h
While new/modified code shouldn't use assert(...) anyway, we had several existing uses of assert. Once we remove nonstd/optional.h, we will no longer implicitly include cassert, implying that a lot of #include <cassert> changes will be necessary. Instead of adding these, go all the way and fix the undesired uses of assert.
1 parent b01b9b7 commit 8934bb1

File tree

108 files changed

+613
-544
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+613
-544
lines changed

jbmc/src/java_bytecode/character_refine_preprocess.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ codet character_refine_preprocesst::convert_char_function(
2929
conversion_inputt &target)
3030
{
3131
const code_function_callt &function_call=target;
32-
assert(function_call.arguments().size()==1);
32+
PRECONDITION(function_call.arguments().size() == 1);
3333
const exprt &arg=function_call.arguments()[0];
3434
const exprt &result=function_call.lhs();
3535
const typet &type=result.type();
@@ -113,7 +113,7 @@ codet character_refine_preprocesst::convert_char_value(
113113
codet character_refine_preprocesst::convert_compare(conversion_inputt &target)
114114
{
115115
const code_function_callt &function_call=target;
116-
assert(function_call.arguments().size()==2);
116+
PRECONDITION(function_call.arguments().size() == 2);
117117
const exprt &char1=function_call.arguments()[0];
118118
const exprt &char2=function_call.arguments()[1];
119119
const exprt &result=function_call.lhs();
@@ -225,7 +225,7 @@ codet character_refine_preprocesst::convert_digit_int(conversion_inputt &target)
225225
codet character_refine_preprocesst::convert_for_digit(conversion_inputt &target)
226226
{
227227
const code_function_callt &function_call=target;
228-
assert(function_call.arguments().size()==2);
228+
PRECONDITION(function_call.arguments().size() == 2);
229229
const exprt &digit=function_call.arguments()[0];
230230
const exprt &result=function_call.lhs();
231231
const typet &type=result.type();
@@ -588,7 +588,7 @@ codet character_refine_preprocesst::convert_is_ideographic(
588588
conversion_inputt &target)
589589
{
590590
const code_function_callt &function_call=target;
591-
assert(function_call.arguments().size()==1);
591+
PRECONDITION(function_call.arguments().size() == 1);
592592
const exprt &arg=function_call.arguments()[0];
593593
const exprt &result=function_call.lhs();
594594
exprt is_ideograph=in_interval_expr(arg, 0x4E00, 0x9FFF);
@@ -602,7 +602,7 @@ codet character_refine_preprocesst::convert_is_ISO_control_char(
602602
conversion_inputt &target)
603603
{
604604
const code_function_callt &function_call=target;
605-
assert(function_call.arguments().size()==1);
605+
PRECONDITION(function_call.arguments().size() == 1);
606606
const exprt &arg=function_call.arguments()[0];
607607
const exprt &result=function_call.lhs();
608608
or_exprt iso(
@@ -760,7 +760,7 @@ codet character_refine_preprocesst::convert_is_low_surrogate(
760760
conversion_inputt &target)
761761
{
762762
const code_function_callt &function_call=target;
763-
assert(function_call.arguments().size()==1);
763+
PRECONDITION(function_call.arguments().size() == 1);
764764
const exprt &arg=function_call.arguments()[0];
765765
const exprt &result=function_call.lhs();
766766
exprt is_low_surrogate=in_interval_expr(arg, 0xDC00, 0xDFFF);
@@ -897,7 +897,7 @@ codet character_refine_preprocesst::convert_is_surrogate_pair(
897897
conversion_inputt &target)
898898
{
899899
const code_function_callt &function_call=target;
900-
assert(function_call.arguments().size()==2);
900+
PRECONDITION(function_call.arguments().size() == 2);
901901
const exprt &arg0=function_call.arguments()[0];
902902
const exprt &arg1=function_call.arguments()[1];
903903
const exprt &result=function_call.lhs();

jbmc/src/java_bytecode/expr2java.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ std::string expr2javat::convert_struct(
117117
const struct_typet::componentst &components=
118118
struct_type.components();
119119

120-
assert(components.size()==src.operands().size());
120+
DATA_INVARIANT(
121+
components.size() == src.operands().size(),
122+
"inconsistent number of components");
121123

122124
exprt::operandst::const_iterator o_it=src.operands().begin();
123125

jbmc/src/java_bytecode/java_bytecode_convert_class.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,8 @@ void java_bytecode_convert_classt::convert(
768768
if(s_it!=symbol_table.symbols.end())
769769
symbol_table.erase(s_it); // erase, we stubbed it
770770

771-
if(symbol_table.add(new_symbol))
772-
assert(false && "failed to add static field symbol");
771+
const bool failed = symbol_table.add(new_symbol);
772+
CHECK_RETURN_WITH_DIAGNOSTICS(!failed, "failed to add static field symbol");
773773
}
774774
else
775775
{

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -780,20 +780,20 @@ code_blockt &java_bytecode_convert_methodt::get_or_create_block_for_pcrange(
780780
bool allow_merge)
781781
{
782782
// Check the tree shape invariant:
783-
assert(tree.branch.size()==tree.branch_addresses.size());
783+
PRECONDITION(tree.branch.size() == tree.branch_addresses.size());
784784

785785
// If there are no child blocks, return this.
786786
if(tree.leaf)
787787
return this_block;
788-
assert(!tree.branch.empty());
788+
PRECONDITION(!tree.branch.empty());
789789

790790
// Find child block starting > address_start:
791791
const auto afterstart=
792792
std::upper_bound(
793793
tree.branch_addresses.begin(),
794794
tree.branch_addresses.end(),
795795
address_start);
796-
assert(afterstart!=tree.branch_addresses.begin());
796+
CHECK_RETURN(afterstart != tree.branch_addresses.begin());
797797
auto findstart=afterstart;
798798
--findstart;
799799
auto child_offset=
@@ -821,9 +821,9 @@ code_blockt &java_bytecode_convert_methodt::get_or_create_block_for_pcrange(
821821
while(child_iter != this_block.statements().end() &&
822822
child_iter->get_statement() == ID_decl)
823823
++child_iter;
824-
assert(child_iter != this_block.statements().end());
824+
CHECK_RETURN(child_iter != this_block.statements().end());
825825
std::advance(child_iter, child_offset);
826-
assert(child_iter != this_block.statements().end());
826+
CHECK_RETURN(child_iter != this_block.statements().end());
827827
auto &child_label=to_code_label(*child_iter);
828828
auto &child_block=to_code_block(child_label.code());
829829

@@ -855,7 +855,7 @@ code_blockt &java_bytecode_convert_methodt::get_or_create_block_for_pcrange(
855855
// Check for incoming control-flow edges targeting non-header
856856
// blocks of the new proposed block range:
857857
auto checkit=amap.find(*findstart);
858-
assert(checkit!=amap.end());
858+
CHECK_RETURN(checkit != amap.end());
859859
++checkit; // Skip the header, which can have incoming edges from outside.
860860
for(;
861861
checkit!=amap.end() && (checkit->first)<(findlim_block_start_address);
@@ -887,15 +887,15 @@ code_blockt &java_bytecode_convert_methodt::get_or_create_block_for_pcrange(
887887
code_labelt newlabel(child_label_name, code_blockt());
888888
code_blockt &newblock=to_code_block(newlabel.code());
889889
auto nblocks=std::distance(findstart, findlim);
890-
assert(nblocks>=2);
890+
CHECK_RETURN(nblocks >= 2);
891891
log.debug() << "Generating codet: combining "
892892
<< std::distance(findstart, findlim) << " blocks for addresses "
893893
<< (*findstart) << "-" << findlim_block_start_address
894894
<< messaget::eom;
895895

896896
// Make a new block containing every child of interest:
897897
auto &this_block_children = this_block.statements();
898-
assert(tree.branch.size()==this_block_children.size());
898+
CHECK_RETURN(tree.branch.size() == this_block_children.size());
899899
for(auto blockidx=child_offset, blocklim=child_offset+nblocks;
900900
blockidx!=blocklim;
901901
++blockidx)
@@ -925,7 +925,7 @@ code_blockt &java_bytecode_convert_methodt::get_or_create_block_for_pcrange(
925925
++branchstart;
926926
tree.branch.erase(branchstart, branchlim);
927927

928-
assert(tree.branch.size()==this_block_children.size());
928+
CHECK_RETURN(tree.branch.size() == this_block_children.size());
929929

930930
auto branchaddriter=tree.branch_addresses.begin();
931931
std::advance(branchaddriter, child_offset);
@@ -941,7 +941,7 @@ code_blockt &java_bytecode_convert_methodt::get_or_create_block_for_pcrange(
941941

942942
tree.branch[child_offset]=std::move(newnode);
943943

944-
assert(tree.branch.size()==tree.branch_addresses.size());
944+
CHECK_RETURN(tree.branch.size() == tree.branch_addresses.size());
945945

946946
return
947947
to_code_block(
@@ -1081,10 +1081,10 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
10811081
converted_instructiont ins=converted_instructiont(i_it, code_skipt());
10821082
std::pair<address_mapt::iterator, bool> a_entry=
10831083
address_map.insert(std::make_pair(i_it->address, ins));
1084-
assert(a_entry.second);
1084+
CHECK_RETURN(a_entry.second);
10851085
// addresses are strictly increasing, hence we must have inserted
10861086
// a new maximal key
1087-
assert(a_entry.first==--address_map.end());
1087+
CHECK_RETURN(a_entry.first == --address_map.end());
10881088

10891089
const auto bytecode = i_it->bytecode;
10901090
const std::string statement = bytecode_info[i_it->bytecode].mnemonic;
@@ -1224,9 +1224,10 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
12241224
instruction.stack.clear();
12251225
codet &c = instruction.code;
12261226

1227-
assert(
1227+
INVARIANT(
12281228
stack.empty() || instruction.predecessors.size() <= 1 ||
1229-
has_prefix(stack.front().get_string(ID_C_base_name), "$stack"));
1229+
has_prefix(stack.front().get_string(ID_C_base_name), "$stack"),
1230+
"inconsistent stack");
12301231

12311232
exprt arg0=i_it->args.size()>=1?i_it->args[0]:nil_exprt();
12321233
exprt arg1=i_it->args.size()>=2?i_it->args[1]:nil_exprt();
@@ -1295,7 +1296,7 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
12951296

12961297
if(bytecode == BC_aconst_null)
12971298
{
1298-
assert(results.size()==1);
1299+
PRECONDITION(results.size() == 1);
12991300
results[0] = null_pointer_exprt(java_reference_type(java_void_type()));
13001301
}
13011302
else if(bytecode == BC_athrow)
@@ -1435,23 +1436,23 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
14351436
// and write something like:
14361437
// if(retaddr==5) goto 5; else if(retaddr==10) goto 10; ...
14371438
PRECONDITION(op.empty() && results.empty());
1438-
assert(!jsr_ret_targets.empty());
1439+
PRECONDITION(!jsr_ret_targets.empty());
14391440
c = convert_ret(
14401441
jsr_ret_targets, arg0, i_it->source_location, i_it->address);
14411442
}
14421443
else if(bytecode == BC_iconst_m1)
14431444
{
1444-
assert(results.size()==1);
1445+
CHECK_RETURN(results.size() == 1);
14451446
results[0]=from_integer(-1, java_int_type());
14461447
}
14471448
else if(bytecode == patternt("?const_?"))
14481449
{
1449-
assert(results.size()==1);
1450+
CHECK_RETURN(results.size() == 1);
14501451
results = convert_const(statement, to_constant_expr(arg0), results);
14511452
}
14521453
else if(bytecode == patternt("?ipush"))
14531454
{
1454-
PRECONDITION(results.size()==1);
1455+
CHECK_RETURN(results.size() == 1);
14551456
DATA_INVARIANT(
14561457
arg0.id()==ID_constant,
14571458
"ipush argument expected to be constant");
@@ -1744,7 +1745,7 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
17441745
numeric_cast_v<std::size_t>(to_constant_expr(arg1));
17451746

17461747
op=pop(dimension);
1747-
assert(results.size()==1);
1748+
CHECK_RETURN(results.size() == 1);
17481749
c = convert_multianewarray(i_it->source_location, arg0, op, results);
17491750
}
17501751
else if(bytecode == BC_arraylength)
@@ -1855,7 +1856,9 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
18551856
stackt::const_iterator os_it=a_it2->second.stack.begin();
18561857
for(auto &expr : stack)
18571858
{
1858-
assert(has_prefix(os_it->get_string(ID_C_base_name), "$stack"));
1859+
INVARIANT(
1860+
has_prefix(os_it->get_string(ID_C_base_name), "$stack"),
1861+
"invalid base name");
18591862
symbol_exprt lhs=to_symbol_expr(*os_it);
18601863
code_assignt a(lhs, expr);
18611864
more_code.add(a);
@@ -1928,7 +1931,7 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
19281931
for(const auto &address_pair : address_map)
19291932
{
19301933
const method_offsett address = address_pair.first;
1931-
assert(address_pair.first==address_pair.second.source->address);
1934+
CHECK_RETURN(address_pair.first == address_pair.second.source->address);
19321935
const codet &c=address_pair.second.code;
19331936

19341937
// Start a new lexical block if this is a branch target:
@@ -1957,9 +1960,10 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
19571960
root_block.add(
19581961
code_labelt{label(std::to_string(address)), code_blockt{}});
19591962
root.branch.push_back(block_tree_nodet::get_leaf());
1960-
assert((root.branch_addresses.empty() ||
1961-
root.branch_addresses.back()<address) &&
1962-
"Block addresses should be unique and increasing");
1963+
INVARIANT(
1964+
(root.branch_addresses.empty() ||
1965+
root.branch_addresses.back() < address),
1966+
"Block addresses should be unique and increasing");
19631967
root.branch_addresses.push_back(address);
19641968
}
19651969

jbmc/src/java_bytecode/java_bytecode_typecheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ std::string java_bytecode_typecheckt::to_string(const typet &type)
2525

2626
void java_bytecode_typecheckt::typecheck_non_type_symbol(symbolt &symbol)
2727
{
28-
assert(!symbol.is_type);
28+
PRECONDITION(!symbol.is_type);
2929
typecheck_type(symbol.type);
3030
typecheck_expr(symbol.value);
3131
}

jbmc/src/java_bytecode/java_class_loader_limit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void java_class_loader_limitt::setup_class_load_limit(
6262
}
6363
else
6464
{
65-
assert(java_cp_include_files.length()>1);
65+
PRECONDITION(java_cp_include_files.length() > 1);
6666
jsont json_cp_config;
6767
if(parse_json(
6868
java_cp_include_files.substr(1),
@@ -76,7 +76,7 @@ void java_class_loader_limitt::setup_class_load_limit(
7676
throw "the JSON file has a wrong format";
7777
for(const jsont &file_entry : to_json_array(include_files))
7878
{
79-
assert(file_entry.is_string());
79+
PRECONDITION(file_entry.is_string());
8080
set_matcher.insert(file_entry.value);
8181
}
8282
}

jbmc/src/java_bytecode/java_entry_point.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ bool java_entry_point(
623623
return true;
624624
symbolt symbol=res.main_function;
625625

626-
assert(symbol.type.id()==ID_code);
626+
DATA_INVARIANT(symbol.type.id() == ID_code, "expected code-typed symbol");
627627

628628
return generate_java_start_function(
629629
symbol,

jbmc/src/java_bytecode/java_local_variable_table.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ static java_bytecode_convert_methodt::method_offsett get_common_dominator(
488488
++domit;
489489
++repeats;
490490
}
491-
assert(repeats<=merge_vars.size());
491+
INVARIANT(repeats <= merge_vars.size(), "out of bounds");
492492
if(repeats==merge_vars.size())
493493
return dom;
494494
}

jbmc/src/java_bytecode/java_pointer_casts.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ bool find_superclass_with_type(
3434
const typet &target_type,
3535
const namespacet &ns)
3636
{
37-
assert(ptr.type().id()==ID_pointer);
37+
PRECONDITION(ptr.type().id() == ID_pointer);
3838
while(true)
3939
{
4040
const typet ptr_base = ns.follow(to_pointer_type(ptr.type()).base_type());
@@ -93,15 +93,15 @@ exprt make_clean_pointer_cast(
9393
exprt bare_ptr=ptr;
9494
while(bare_ptr.id()==ID_typecast)
9595
{
96-
assert(
97-
bare_ptr.type().id()==ID_pointer &&
96+
INVARIANT(
97+
bare_ptr.type().id() == ID_pointer,
9898
"Non-pointer in make_clean_pointer_cast?");
9999
if(to_pointer_type(bare_ptr.type()).base_type() == java_void_type())
100100
bare_ptr = to_typecast_expr(bare_ptr).op();
101101
}
102102

103-
assert(
104-
bare_ptr.type().id()==ID_pointer &&
103+
INVARIANT(
104+
bare_ptr.type().id() == ID_pointer,
105105
"Non-pointer in make_clean_pointer_cast?");
106106

107107
if(bare_ptr.type()==target_type)

jbmc/src/java_bytecode/java_types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,13 +581,13 @@ class java_class_typet:public class_typet
581581

582582
inline const java_class_typet &to_java_class_type(const typet &type)
583583
{
584-
assert(type.id()==ID_struct);
584+
PRECONDITION(type.id() == ID_struct);
585585
return static_cast<const java_class_typet &>(type);
586586
}
587587

588588
inline java_class_typet &to_java_class_type(typet &type)
589589
{
590-
assert(type.id()==ID_struct);
590+
PRECONDITION(type.id() == ID_struct);
591591
return static_cast<java_class_typet &>(type);
592592
}
593593

src/analyses/custom_bitvector_analysis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ void custom_bitvector_domaint::output(
579579
for(unsigned i=0; b!=0; i++, b>>=1)
580580
if(b&1)
581581
{
582-
assert(i<cba.bits.size());
582+
INVARIANT(i < cba.bits.size(), "inconsistent bit widths");
583583
out << ' '
584584
<< cba.bits[i];
585585
}
@@ -595,7 +595,7 @@ void custom_bitvector_domaint::output(
595595
for(unsigned i=0; b!=0; i++, b>>=1)
596596
if(b&1)
597597
{
598-
assert(i<cba.bits.size());
598+
INVARIANT(i < cba.bits.size(), "inconsistent bit widths");
599599
out << ' '
600600
<< cba.bits[i];
601601
}

0 commit comments

Comments
 (0)