Skip to content

Commit e2cfed3

Browse files
committed
Use invariant annotations instead of asserts
1 parent 38b4c48 commit e2cfed3

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

src/goto-instrument/dump_c.cpp

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Daniel Kroening, kroening@kroening.com
1313
#include <cctype>
1414

1515
#include <util/config.h>
16+
#include <util/invariant.h>
1617
#include <util/prefix.h>
1718
#include <util/suffix.h>
1819
#include <util/find_symbols.h>
@@ -99,14 +100,14 @@ void dump_ct::operator()(std::ostream &os)
99100
if((symbol.type.id()==ID_union || symbol.type.id()==ID_struct) &&
100101
symbol.type.get(ID_tag).empty())
101102
{
102-
assert(symbol.is_type);
103+
PRECONDITION(symbol.is_type);
103104
symbol.type.set(ID_tag, ID_anonymous);
104105
tag_added=true;
105106
}
106107
else if(symbol.type.id()==ID_c_enum &&
107108
symbol.type.find(ID_tag).get(ID_C_base_name).empty())
108109
{
109-
assert(symbol.is_type);
110+
PRECONDITION(symbol.is_type);
110111
symbol.type.add(ID_tag).set(ID_C_base_name, ID_anonymous);
111112
tag_added=true;
112113
}
@@ -150,8 +151,8 @@ void dump_ct::operator()(std::ostream &os)
150151
if((!tag_added || symbol.is_type) && ignore(symbol))
151152
continue;
152153

153-
if(!symbols_sorted.insert(name_str).second)
154-
assert(false);
154+
bool inserted=symbols_sorted.insert(name_str).second;
155+
CHECK_RETURN(inserted);
155156
}
156157

157158
gather_global_typedefs();
@@ -314,7 +315,7 @@ void dump_ct::convert_compound(
314315
{
315316
const symbolt &symbol=
316317
ns.lookup(to_symbol_type(type).get_identifier());
317-
assert(symbol.is_type);
318+
DATA_INVARIANT(symbol.is_type, "symbol expected to be type symbol");
318319

319320
if(!ignore(symbol))
320321
convert_compound(symbol.type, unresolved, recursive, os);
@@ -323,7 +324,7 @@ void dump_ct::convert_compound(
323324
{
324325
const symbolt &symbol=
325326
ns.lookup(to_c_enum_tag_type(type).get_identifier());
326-
assert(symbol.is_type);
327+
DATA_INVARIANT(symbol.is_type, "symbol expected to be type symbol");
327328

328329
if(!ignore(symbol))
329330
convert_compound(symbol.type, unresolved, recursive, os);
@@ -375,7 +376,7 @@ void dump_ct::convert_compound(
375376
std::stringstream base_decls;
376377
forall_irep(parent_it, bases.get_sub())
377378
{
378-
assert(false);
379+
UNREACHABLE;
379380
/*
380381
assert(parent_it->id() == ID_base);
381382
assert(parent_it->get(ID_type) == ID_symbol);
@@ -446,7 +447,7 @@ void dump_ct::convert_compound(
446447
// namespace
447448
std::string fake_unique_name="NO/SUCH/NS::"+id2string(comp_name);
448449
std::string s=make_decl(fake_unique_name, comp.type());
449-
assert(s.find("NO/SUCH/NS")==std::string::npos);
450+
POSTCONDITION(s.find("NO/SUCH/NS")==std::string::npos);
450451

451452
if(comp_type.id()==ID_c_bit_field &&
452453
to_c_bit_field_type(comp_type).get_width()==0)
@@ -484,7 +485,7 @@ void dump_ct::convert_compound(
484485
struct_body << s;
485486
}
486487
else
487-
assert(false);
488+
UNREACHABLE;
488489

489490
struct_body << ";\n";
490491
}
@@ -499,7 +500,7 @@ void dump_ct::convert_compound(
499500
typedef_str=td_entry->second;
500501
std::pair<typedef_mapt::iterator, bool> td_map_entry=
501502
typedef_map.insert({typedef_str, typedef_infot(typedef_str)});
502-
assert(!td_map_entry.second);
503+
PRECONDITION(!td_map_entry.second);
503504
if(!td_map_entry.first->second.early)
504505
td_map_entry.first->second.type_decl_str="";
505506
os << "typedef ";
@@ -508,7 +509,7 @@ void dump_ct::convert_compound(
508509
os << type_to_string(unresolved_clean);
509510
if(!base_decls.str().empty())
510511
{
511-
assert(language->id()=="cpp");
512+
PRECONDITION(language->id()=="cpp");
512513
os << ": " << base_decls.str();
513514
}
514515
os << '\n';
@@ -541,7 +542,7 @@ void dump_ct::convert_compound_enum(
541542
const typet &type,
542543
std::ostream &os)
543544
{
544-
assert(type.id()==ID_c_enum);
545+
PRECONDITION(type.id()==ID_c_enum);
545546

546547
const irept &tag=type.find(ID_tag);
547548
const irep_idt &name=tag.get(ID_C_base_name);
@@ -899,7 +900,7 @@ void dump_ct::cleanup_decl(
899900
system_headers);
900901
p2s();
901902

902-
assert(b.operands().size()==1);
903+
POSTCONDITION(b.operands().size()==1);
903904
decl.swap(b.op0());
904905
}
905906

@@ -1006,7 +1007,7 @@ void dump_ct::collect_typedefs_rec(
10061007
for(const auto &d : local_deps)
10071008
{
10081009
auto td_entry=typedef_map.find(d);
1009-
assert(td_entry!=typedef_map.end());
1010+
PRECONDITION(td_entry!=typedef_map.end());
10101011
td_entry->second.early=true;
10111012
}
10121013
}
@@ -1049,7 +1050,7 @@ void dump_ct::gather_global_typedefs()
10491050
symbol.location.get_function().empty())
10501051
{
10511052
const irep_idt &typedef_str=symbol.type.get(ID_C_typedef);
1052-
assert(!typedef_str.empty());
1053+
PRECONDITION(!typedef_str.empty());
10531054
typedef_types[symbol.type]=typedef_str;
10541055
if(ignore(symbol))
10551056
typedef_map.insert({typedef_str, typedef_infot(typedef_str)});
@@ -1131,14 +1132,14 @@ void dump_ct::dump_typedefs(std::ostream &os) const
11311132

11321133
// update dependencies
11331134
id_sett &f_deps=f_it->second;
1134-
assert(!f_deps.empty());
1135-
assert(f_deps.find(t.typedef_name)!=f_deps.end());
1135+
PRECONDITION(!f_deps.empty());
1136+
PRECONDITION(f_deps.find(t.typedef_name)!=f_deps.end());
11361137
f_deps.erase(t.typedef_name);
11371138

11381139
if(f_deps.empty()) // all depenencies done now!
11391140
{
11401141
const auto td_entry=typedef_map.find(*it);
1141-
assert(td_entry!=typedef_map.end());
1142+
PRECONDITION(td_entry!=typedef_map.end());
11421143
to_insert.insert({id2string(*it), td_entry->second});
11431144
forward_deps.erase(*it);
11441145
it=r_deps.erase(it);
@@ -1148,7 +1149,7 @@ void dump_ct::dump_typedefs(std::ostream &os) const
11481149
}
11491150
}
11501151

1151-
assert(forward_deps.empty());
1152+
POSTCONDITION(forward_deps.empty());
11521153

11531154
for(const auto &td : typedefs_sorted)
11541155
os << td.type_decl_str << '\n';
@@ -1190,8 +1191,10 @@ void dump_ct::convert_global_variable(
11901191
it=syms.begin();
11911192
it!=syms.end();
11921193
++it)
1193-
if(!symbols_sorted.insert(id2string(*it)).second)
1194-
assert(false);
1194+
{
1195+
bool inserted=symbols_sorted.insert(id2string(*it)).second;
1196+
CHECK_RETURN(inserted);
1197+
}
11951198

11961199
for(std::set<std::string>::const_iterator
11971200
it=symbols_sorted.begin();
@@ -1215,7 +1218,7 @@ void dump_ct::convert_global_variable(
12151218

12161219
std::list<irep_idt> empty_static, empty_types;
12171220
cleanup_decl(d, empty_static, empty_types);
1218-
assert(empty_static.empty());
1221+
CHECK_RETURN(empty_static.empty());
12191222
os << expr_to_string(d) << '\n';
12201223
}
12211224
}
@@ -1391,7 +1394,7 @@ void dump_ct::insert_local_static_decls(
13911394
{
13921395
local_static_declst::const_iterator d_it=
13931396
local_static_decls.find(*it);
1394-
assert(d_it!=local_static_decls.end());
1397+
PRECONDITION(d_it!=local_static_decls.end());
13951398

13961399
code_declt d=d_it->second;
13971400
std::list<irep_idt> redundant;
@@ -1404,7 +1407,7 @@ void dump_ct::insert_local_static_decls(
14041407
// within an if(false) { ... } block
14051408
if(find_block_position_rec(*it, b, dest_ptr, before))
14061409
{
1407-
assert(dest_ptr!=0);
1410+
CHECK_RETURN(dest_ptr!=0);
14081411
dest_ptr->operands().insert(before, d);
14091412
}
14101413
}
@@ -1441,7 +1444,7 @@ void dump_ct::insert_local_type_decls(
14411444
// has been removed by cleanup operations
14421445
if(find_block_position_rec(*it, b, dest_ptr, before))
14431446
{
1444-
assert(dest_ptr!=0);
1447+
CHECK_RETURN(dest_ptr!=0);
14451448
dest_ptr->operands().insert(before, skip);
14461449
}
14471450
}
@@ -1465,7 +1468,7 @@ void dump_ct::cleanup_expr(exprt &expr)
14651468
exprt::operandst old_ops;
14661469
old_ops.swap(expr.operands());
14671470

1468-
assert(old_components.size()==old_ops.size());
1471+
PRECONDITION(old_components.size()==old_ops.size());
14691472
exprt::operandst::iterator o_it=old_ops.begin();
14701473
for(struct_union_typet::componentst::const_iterator
14711474
it=old_components.begin();
@@ -1507,7 +1510,7 @@ void dump_ct::cleanup_expr(exprt &expr)
15071510
const struct_union_typet::componentt &comp=
15081511
u_type_f.get_component(u.get_component_name());
15091512
const typet &u_op_type=comp.type();
1510-
assert(u_op_type.id()==ID_pointer);
1513+
PRECONDITION(u_op_type.id()==ID_pointer);
15111514

15121515
typecast_exprt tc(u.op(), u_op_type);
15131516
expr.swap(tc);
@@ -1615,8 +1618,9 @@ void dump_ct::cleanup_type(typet &type)
16151618
if(type.id()==ID_array)
16161619
cleanup_expr(to_array_type(type).size());
16171620

1618-
assert((type.id()!=ID_union && type.id()!=ID_struct) ||
1619-
!type.get(ID_tag).empty());
1621+
POSTCONDITION(
1622+
(type.id()!=ID_union && type.id()!=ID_struct) ||
1623+
!type.get(ID_tag).empty());
16201624
}
16211625

16221626
std::string dump_ct::type_to_string(const typet &type)

0 commit comments

Comments
 (0)