@@ -103,10 +103,50 @@ void goto_trace_stept::output(
103
103
104
104
out << ' \n ' ;
105
105
}
106
+ // / Returns the numeric representation of an expression, based on
107
+ // / options. The default is binary without a base-prefix. Setting
108
+ // / options.hex_representation to be true outputs hex format. Setting
109
+ // / options.base_prefix to be true appends either 0b or 0x to the number
110
+ // / to indicate the base
111
+ // / \param expr: expression to get numeric representation from
112
+ // / \param options: configuration options
113
+ // / \return a string with the numeric representation
114
+ static std::string
115
+ numeric_representation (const exprt &expr, const trace_optionst &options)
116
+ {
117
+ std::string result;
118
+ std::string prefix;
119
+ if (options.hex_representation )
120
+ {
121
+ mp_integer value_int =
122
+ binary2integer (id2string (to_constant_expr (expr).get_value ()), false );
123
+ result = integer2string (value_int, 16 );
124
+ prefix = " 0x" ;
125
+ }
126
+ else
127
+ {
128
+ prefix = " 0b" ;
129
+ result = expr.get_string (ID_value);
130
+ }
106
131
107
- std::string trace_value_binary (
132
+ std::ostringstream oss;
133
+ std::string::size_type i = 0 ;
134
+ for (const auto c : result)
135
+ {
136
+ oss << c;
137
+ if (++i % 8 == 0 && result.size () != i)
138
+ oss << ' ' ;
139
+ }
140
+ if (options.base_prefix )
141
+ return prefix + oss.str ();
142
+ else
143
+ return oss.str ();
144
+ }
145
+
146
+ std::string trace_numeric_value (
108
147
const exprt &expr,
109
- const namespacet &ns)
148
+ const namespacet &ns,
149
+ const trace_optionst &options)
110
150
{
111
151
const typet &type=ns.follow (expr.type ());
112
152
@@ -123,18 +163,8 @@ std::string trace_value_binary(
123
163
type.id ()==ID_c_enum ||
124
164
type.id ()==ID_c_enum_tag)
125
165
{
126
- const std::string & str = expr.get_string (ID_value);
127
-
128
- std::ostringstream oss;
129
- std::string::size_type i = 0 ;
130
- for (const auto c : str)
131
- {
132
- oss << c;
133
- if (++i % 8 == 0 && str.size () != i)
134
- oss << ' ' ;
135
- }
136
-
137
- return oss.str ();
166
+ const std::string &str = numeric_representation (expr, options);
167
+ return str;
138
168
}
139
169
else if (type.id ()==ID_bool)
140
170
{
@@ -157,7 +187,7 @@ std::string trace_value_binary(
157
187
result=" { " ;
158
188
else
159
189
result+=" , " ;
160
- result+=trace_value_binary (*it, ns);
190
+ result+=trace_numeric_value (*it, ns, options );
161
191
}
162
192
163
193
return result+" }" ;
@@ -170,15 +200,15 @@ std::string trace_value_binary(
170
200
{
171
201
if (it!=expr.operands ().begin ())
172
202
result+=" , " ;
173
- result+=trace_value_binary (*it, ns);
203
+ result+=trace_numeric_value (*it, ns, options );
174
204
}
175
205
176
206
return result+" }" ;
177
207
}
178
208
else if (expr.id ()==ID_union)
179
209
{
180
- assert (expr.operands ().size ()==1 );
181
- return trace_value_binary (expr.op0 (), ns);
210
+ PRECONDITION (expr.operands ().size ()==1 );
211
+ return trace_numeric_value (expr.op0 (), ns, options );
182
212
}
183
213
184
214
return " ?" ;
@@ -189,7 +219,8 @@ void trace_value(
189
219
const namespacet &ns,
190
220
const ssa_exprt &lhs_object,
191
221
const exprt &full_lhs,
192
- const exprt &value)
222
+ const exprt &value,
223
+ const trace_optionst &options)
193
224
{
194
225
irep_idt identifier;
195
226
@@ -205,7 +236,7 @@ void trace_value(
205
236
value_string=from_expr (ns, identifier, value);
206
237
207
238
// the binary representation
208
- value_string+= " (" + trace_value_binary (value, ns)+ " )" ;
239
+ value_string += " (" + trace_numeric_value (value, ns, options) + " )" ;
209
240
}
210
241
211
242
out << " "
@@ -247,7 +278,8 @@ bool is_index_member_symbol(const exprt &src)
247
278
void show_goto_trace (
248
279
std::ostream &out,
249
280
const namespacet &ns,
250
- const goto_tracet &goto_trace)
281
+ const goto_tracet &goto_trace,
282
+ const trace_optionst &options)
251
283
{
252
284
unsigned prev_step_nr=0 ;
253
285
bool first_step=true ;
@@ -315,10 +347,20 @@ void show_goto_trace(
315
347
// see if the full lhs is something clean
316
348
if (is_index_member_symbol (step.full_lhs ))
317
349
trace_value (
318
- out, ns, step.lhs_object , step.full_lhs , step.full_lhs_value );
350
+ out,
351
+ ns,
352
+ step.lhs_object ,
353
+ step.full_lhs ,
354
+ step.full_lhs_value ,
355
+ options);
319
356
else
320
357
trace_value (
321
- out, ns, step.lhs_object , step.lhs_object , step.lhs_object_value );
358
+ out,
359
+ ns,
360
+ step.lhs_object ,
361
+ step.lhs_object ,
362
+ step.lhs_object_value ,
363
+ options);
322
364
}
323
365
break ;
324
366
@@ -330,7 +372,8 @@ void show_goto_trace(
330
372
show_state_header (out, step, step.pc ->source_location , step.step_nr );
331
373
}
332
374
333
- trace_value (out, ns, step.lhs_object , step.full_lhs , step.full_lhs_value );
375
+ trace_value (
376
+ out, ns, step.lhs_object , step.full_lhs , step.full_lhs_value , options);
334
377
break ;
335
378
336
379
case goto_trace_stept::typet::OUTPUT:
@@ -356,7 +399,7 @@ void show_goto_trace(
356
399
out << " " << from_expr (ns, step.pc ->function , *l_it);
357
400
358
401
// the binary representation
359
- out << " (" << trace_value_binary (*l_it, ns) << " )" ;
402
+ out << " (" << trace_numeric_value (*l_it, ns, options ) << " )" ;
360
403
}
361
404
362
405
out << " \n " ;
@@ -377,7 +420,7 @@ void show_goto_trace(
377
420
out << " " << from_expr (ns, step.pc ->function , *l_it);
378
421
379
422
// the binary representation
380
- out << " (" << trace_value_binary (*l_it, ns) << " )" ;
423
+ out << " (" << trace_numeric_value (*l_it, ns, options ) << " )" ;
381
424
}
382
425
383
426
out << " \n " ;
@@ -401,5 +444,12 @@ void show_goto_trace(
401
444
}
402
445
}
403
446
447
+ void show_goto_trace (
448
+ std::ostream &out,
449
+ const namespacet &ns,
450
+ const goto_tracet &goto_trace)
451
+ {
452
+ show_goto_trace (out, ns, goto_trace, trace_optionst::default_options);
453
+ }
404
454
405
455
const trace_optionst trace_optionst::default_options = trace_optionst();
0 commit comments