Skip to content

Commit 58b990d

Browse files
committed
Use from_{expr,type} matching the language of the expression/type
For debugging output it's good enough to use the language-agnostic format(...). In a few cases we do not have sufficient context to determine the mode/language, which are now annotated with comments.
1 parent 0bff19b commit 58b990d

32 files changed

+189
-168
lines changed

src/analyses/constant_propagator.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Peter Schrammel
1313

1414
#ifdef DEBUG
1515
#include <iostream>
16+
#include <util/format_expr.h>
1617
#endif
1718

1819
#include <util/find_symbols.h>
@@ -219,7 +220,7 @@ bool constant_propagator_domaint::two_way_propagate_rec(
219220
const namespacet &ns)
220221
{
221222
#ifdef DEBUG
222-
std::cout << "two_way_propagate_rec: " << from_expr(ns, "", expr) << '\n';
223+
std::cout << "two_way_propagate_rec: " << format(expr) << '\n';
223224
#endif
224225

225226
bool change=false;
@@ -379,7 +380,7 @@ void constant_propagator_domaint::valuest::output(
379380

380381
for(const auto &p : replace_const.expr_map)
381382
{
382-
out << ' ' << p.first << "=" << from_expr(ns, "", p.second) << '\n';
383+
out << ' ' << p.first << "=" << from_expr(ns, p.first, p.second) << '\n';
383384
}
384385
}
385386

src/analyses/goto_check.cpp

+35-38
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Author: Daniel Kroening, [email protected]
2828
#include <util/cprover_prefix.h>
2929
#include <util/options.h>
3030

31-
#include <langapi/language_util.h>
31+
#include <langapi/language.h>
32+
#include <langapi/mode.h>
3233

3334
#include "local_bitvector_analysis.h"
3435

@@ -80,9 +81,8 @@ class goto_checkt
8081
void check_rec(
8182
const exprt &expr,
8283
guardt &guard,
83-
bool address,
84-
const irep_idt &mode);
85-
void check(const exprt &expr, const irep_idt &mode);
84+
bool address);
85+
void check(const exprt &expr);
8686

8787
void bounds_check(const index_exprt &expr, const guardt &guard);
8888
void div_by_zero_check(const div_exprt &expr, const guardt &guard);
@@ -94,8 +94,7 @@ class goto_checkt
9494
const dereference_exprt &expr,
9595
const guardt &guard,
9696
const exprt &access_lb,
97-
const exprt &access_ub,
98-
const irep_idt &mode);
97+
const exprt &access_ub);
9998
void integer_overflow_check(const exprt &expr, const guardt &guard);
10099
void conversion_check(const exprt &expr, const guardt &guard);
101100
void float_overflow_check(const exprt &expr, const guardt &guard);
@@ -141,6 +140,8 @@ class goto_checkt
141140
typedef std::pair<exprt, exprt> allocationt;
142141
typedef std::list<allocationt> allocationst;
143142
allocationst allocations;
143+
144+
irep_idt mode;
144145
};
145146

146147
void goto_checkt::collect_allocations(
@@ -917,8 +918,7 @@ void goto_checkt::pointer_validity_check(
917918
const dereference_exprt &expr,
918919
const guardt &guard,
919920
const exprt &access_lb,
920-
const exprt &access_ub,
921-
const irep_idt &mode)
921+
const exprt &access_ub)
922922
{
923923
if(!enable_pointer_check)
924924
return;
@@ -1274,7 +1274,8 @@ void goto_checkt::add_guarded_claim(
12741274

12751275
goto_programt::targett t=new_code.add_instruction(type);
12761276

1277-
std::string source_expr_string=from_expr(ns, "", src_expr);
1277+
std::string source_expr_string;
1278+
get_language_from_mode(mode)->from_expr(src_expr, source_expr_string, ns);
12781279

12791280
t->guard.swap(new_expr);
12801281
t->source_location=source_location;
@@ -1283,11 +1284,7 @@ void goto_checkt::add_guarded_claim(
12831284
}
12841285
}
12851286

1286-
void goto_checkt::check_rec(
1287-
const exprt &expr,
1288-
guardt &guard,
1289-
bool address,
1290-
const irep_idt &mode)
1287+
void goto_checkt::check_rec(const exprt &expr, guardt &guard, bool address)
12911288
{
12921289
// we don't look into quantifiers
12931290
if(expr.id()==ID_exists || expr.id()==ID_forall)
@@ -1298,26 +1295,26 @@ void goto_checkt::check_rec(
12981295
if(expr.id()==ID_dereference)
12991296
{
13001297
assert(expr.operands().size()==1);
1301-
check_rec(expr.op0(), guard, false, mode);
1298+
check_rec(expr.op0(), guard, false);
13021299
}
13031300
else if(expr.id()==ID_index)
13041301
{
13051302
assert(expr.operands().size()==2);
1306-
check_rec(expr.op0(), guard, true, mode);
1307-
check_rec(expr.op1(), guard, false, mode);
1303+
check_rec(expr.op0(), guard, true);
1304+
check_rec(expr.op1(), guard, false);
13081305
}
13091306
else
13101307
{
13111308
forall_operands(it, expr)
1312-
check_rec(*it, guard, true, mode);
1309+
check_rec(*it, guard, true);
13131310
}
13141311
return;
13151312
}
13161313

13171314
if(expr.id()==ID_address_of)
13181315
{
13191316
assert(expr.operands().size()==1);
1320-
check_rec(expr.op0(), guard, true, mode);
1317+
check_rec(expr.op0(), guard, true);
13211318
return;
13221319
}
13231320
else if(expr.id()==ID_and || expr.id()==ID_or)
@@ -1334,7 +1331,7 @@ void goto_checkt::check_rec(
13341331
throw "`"+expr.id_string()+"' takes Boolean operands only, but got "+
13351332
op.pretty();
13361333

1337-
check_rec(op, guard, false, mode);
1334+
check_rec(op, guard, false);
13381335

13391336
if(expr.id()==ID_or)
13401337
guard.add(not_exprt(op));
@@ -1359,19 +1356,19 @@ void goto_checkt::check_rec(
13591356
throw msg;
13601357
}
13611358

1362-
check_rec(expr.op0(), guard, false, mode);
1359+
check_rec(expr.op0(), guard, false);
13631360

13641361
{
13651362
guardt old_guard=guard;
13661363
guard.add(expr.op0());
1367-
check_rec(expr.op1(), guard, false, mode);
1364+
check_rec(expr.op1(), guard, false);
13681365
guard.swap(old_guard);
13691366
}
13701367

13711368
{
13721369
guardt old_guard=guard;
13731370
guard.add(not_exprt(expr.op0()));
1374-
check_rec(expr.op2(), guard, false, mode);
1371+
check_rec(expr.op2(), guard, false);
13751372
guard.swap(old_guard);
13761373
}
13771374

@@ -1384,7 +1381,7 @@ void goto_checkt::check_rec(
13841381
const dereference_exprt &deref=
13851382
to_dereference_expr(member.struct_op());
13861383

1387-
check_rec(deref.op0(), guard, false, mode);
1384+
check_rec(deref.op0(), guard, false);
13881385

13891386
exprt access_ub=nil_exprt();
13901387

@@ -1394,13 +1391,13 @@ void goto_checkt::check_rec(
13941391
if(member_offset.is_not_nil() && size.is_not_nil())
13951392
access_ub=plus_exprt(member_offset, size);
13961393

1397-
pointer_validity_check(deref, guard, member_offset, access_ub, mode);
1394+
pointer_validity_check(deref, guard, member_offset, access_ub);
13981395

13991396
return;
14001397
}
14011398

14021399
forall_operands(it, expr)
1403-
check_rec(*it, guard, false, mode);
1400+
check_rec(*it, guard, false);
14041401

14051402
if(expr.id()==ID_index)
14061403
{
@@ -1462,21 +1459,21 @@ void goto_checkt::check_rec(
14621459
to_dereference_expr(expr),
14631460
guard,
14641461
nil_exprt(),
1465-
size_of_expr(expr.type(), ns),
1466-
mode);
1462+
size_of_expr(expr.type(), ns));
14671463
}
14681464

1469-
void goto_checkt::check(const exprt &expr, const irep_idt &mode)
1465+
void goto_checkt::check(const exprt &expr)
14701466
{
14711467
guardt guard;
1472-
check_rec(expr, guard, false, mode);
1468+
check_rec(expr, guard, false);
14731469
}
14741470

14751471
void goto_checkt::goto_check(
14761472
goto_functiont &goto_function,
1477-
const irep_idt &mode)
1473+
const irep_idt &_mode)
14781474
{
14791475
assertions.clear();
1476+
mode = _mode;
14801477

14811478
local_bitvector_analysist local_bitvector_analysis_obj(goto_function);
14821479
local_bitvector_analysis=&local_bitvector_analysis_obj;
@@ -1497,7 +1494,7 @@ void goto_checkt::goto_check(
14971494
i.is_target())
14981495
assertions.clear();
14991496

1500-
check(i.guard, mode);
1497+
check(i.guard);
15011498

15021499
// magic ERROR label?
15031500
for(const auto &label : error_labels)
@@ -1523,20 +1520,20 @@ void goto_checkt::goto_check(
15231520

15241521
if(statement==ID_expression)
15251522
{
1526-
check(i.code, mode);
1523+
check(i.code);
15271524
}
15281525
else if(statement==ID_printf)
15291526
{
15301527
forall_operands(it, i.code)
1531-
check(*it, mode);
1528+
check(*it);
15321529
}
15331530
}
15341531
else if(i.is_assign())
15351532
{
15361533
const code_assignt &code_assign=to_code_assign(i.code);
15371534

1538-
check(code_assign.lhs(), mode);
1539-
check(code_assign.rhs(), mode);
1535+
check(code_assign.lhs());
1536+
check(code_assign.rhs());
15401537

15411538
// the LHS might invalidate any assertion
15421539
invalidate(code_assign.lhs());
@@ -1576,7 +1573,7 @@ void goto_checkt::goto_check(
15761573
}
15771574

15781575
forall_operands(it, code_function_call)
1579-
check(*it, mode);
1576+
check(*it);
15801577

15811578
// the call might invalidate any assertion
15821579
assertions.clear();
@@ -1585,7 +1582,7 @@ void goto_checkt::goto_check(
15851582
{
15861583
if(i.code.operands().size()==1)
15871584
{
1588-
check(i.code.op0(), mode);
1585+
check(i.code.op0());
15891586
// the return value invalidate any assertion
15901587
invalidate(i.code.op0());
15911588
}

src/analyses/goto_rw.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ void guarded_range_domaint::output(
633633
if(itr!=begin())
634634
out << ";";
635635
out << itr->first << ":" << itr->second.first;
636+
// we don't know what mode (language) we are in, so we rely on the default
637+
// language to be reasonable for from_expr
636638
out << " if " << from_expr(ns, "", itr->second.second);
637639
}
638640
out << "]";

src/analyses/invariant_set.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ std::string inv_object_storet::build_string(const exprt &expr) const
131131

132132
// we also like "address_of" and "reference_to"
133133
// if the object is constant
134+
// we don't know what mode (language) we are in, so we rely on the default
135+
// language to be reasonable for from_expr
134136
if(is_constant_address(expr))
135137
return from_expr(ns, "", expr);
136138

@@ -884,7 +886,6 @@ std::string inv_object_storet::to_string(
884886
unsigned a,
885887
const irep_idt &identifier) const
886888
{
887-
// return from_expr(ns, "", get_expr(a));
888889
return id2string(map[a]);
889890
}
890891

src/analyses/local_may_alias.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,10 @@ void local_may_aliast::output(
457457
if(loc_info.aliases.find(j)==i)
458458
{
459459
assert(j<objects.size());
460-
out << ' ' << from_expr(ns, "", objects[j]);
460+
irep_idt identifier = "";
461+
if(objects[j].id() == ID_symbol)
462+
identifier = to_symbol_expr(objects[j]).get_identifier();
463+
out << ' ' << from_expr(ns, identifier, objects[j]);
461464
}
462465

463466
out << " }";

src/cbmc/bmc.cpp

+10-18
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,16 @@ void bmct::show_program()
239239
{
240240
std::cout << "// " << step.source.pc->location_number << " ";
241241
std::cout << step.source.pc->source_location.as_string() << "\n";
242+
const irep_idt &function = step.source.pc->function;
242243

243244
if(step.is_assignment())
244245
{
245-
std::string string_value=
246-
from_expr(ns, "", step.cond_expr);
246+
std::string string_value = from_expr(ns, function, step.cond_expr);
247247
std::cout << "(" << count << ") " << string_value << "\n";
248248

249249
if(!step.guard.is_true())
250250
{
251-
std::string string_value=
252-
from_expr(ns, "", step.guard);
251+
std::string string_value = from_expr(ns, function, step.guard);
253252
std::cout << std::string(std::to_string(count).size()+3, ' ');
254253
std::cout << "guard: " << string_value << "\n";
255254
}
@@ -258,15 +257,13 @@ void bmct::show_program()
258257
}
259258
else if(step.is_assert())
260259
{
261-
std::string string_value=
262-
from_expr(ns, "", step.cond_expr);
260+
std::string string_value = from_expr(ns, function, step.cond_expr);
263261
std::cout << "(" << count << ") ASSERT("
264262
<< string_value <<") " << "\n";
265263

266264
if(!step.guard.is_true())
267265
{
268-
std::string string_value=
269-
from_expr(ns, "", step.guard);
266+
std::string string_value = from_expr(ns, function, step.guard);
270267
std::cout << std::string(std::to_string(count).size()+3, ' ');
271268
std::cout << "guard: " << string_value << "\n";
272269
}
@@ -275,15 +272,13 @@ void bmct::show_program()
275272
}
276273
else if(step.is_assume())
277274
{
278-
std::string string_value=
279-
from_expr(ns, "", step.cond_expr);
275+
std::string string_value = from_expr(ns, function, step.cond_expr);
280276
std::cout << "(" << count << ") ASSUME("
281277
<< string_value <<") " << "\n";
282278

283279
if(!step.guard.is_true())
284280
{
285-
std::string string_value=
286-
from_expr(ns, "", step.guard);
281+
std::string string_value = from_expr(ns, function, step.guard);
287282
std::cout << std::string(std::to_string(count).size()+3, ' ');
288283
std::cout << "guard: " << string_value << "\n";
289284
}
@@ -292,25 +287,22 @@ void bmct::show_program()
292287
}
293288
else if(step.is_constraint())
294289
{
295-
std::string string_value=
296-
from_expr(ns, "", step.cond_expr);
290+
std::string string_value = from_expr(ns, function, step.cond_expr);
297291
std::cout << "(" << count << ") CONSTRAINT("
298292
<< string_value <<") " << "\n";
299293

300294
count++;
301295
}
302296
else if(step.is_shared_read() || step.is_shared_write())
303297
{
304-
std::string string_value=
305-
from_expr(ns, "", step.ssa_lhs);
298+
std::string string_value = from_expr(ns, function, step.ssa_lhs);
306299
std::cout << "(" << count << ") SHARED_"
307300
<< (step.is_shared_write()?"WRITE":"READ")
308301
<< "(" << string_value <<")\n";
309302

310303
if(!step.guard.is_true())
311304
{
312-
std::string string_value=
313-
from_expr(ns, "", step.guard);
305+
std::string string_value = from_expr(ns, function, step.guard);
314306
std::cout << std::string(std::to_string(count).size()+3, ' ');
315307
std::cout << "guard: " << string_value << "\n";
316308
}

0 commit comments

Comments
 (0)