@@ -11,279 +11,4 @@ Author: Daniel Kroening
11
11
// / \file
12
12
// / Traces of GOTO Programs
13
13
14
- #include " json_goto_trace.h"
15
14
16
- #include < util/json_expr.h>
17
- #include < util/arith_tools.h>
18
- #include < util/config.h>
19
- #include < util/invariant.h>
20
- #include < util/simplify_expr.h>
21
-
22
- #include < langapi/language_util.h>
23
- #include < util/json_irep.h>
24
-
25
- // / Produce a json representation of a trace.
26
- // / \param ns: a namespace
27
- // / \param goto_trace: a trace in a goto program
28
- // / \param dest: referecence to a json object in which the goto trace will be
29
- // / added
30
- void convert (
31
- const namespacet &ns,
32
- const goto_tracet &goto_trace,
33
- jsont &dest,
34
- trace_optionst trace_options)
35
- {
36
- json_arrayt &dest_array=dest.make_array ();
37
-
38
- source_locationt previous_source_location;
39
-
40
- for (const auto &step : goto_trace.steps )
41
- {
42
- const source_locationt &source_location=step.pc ->source_location ;
43
-
44
- jsont json_location;
45
-
46
- if (source_location.is_not_nil () && source_location.get_file ()!=" " )
47
- json_location=json (source_location);
48
- else
49
- json_location=json_nullt ();
50
-
51
- switch (step.type )
52
- {
53
- case goto_trace_stept::typet::ASSERT:
54
- if (!step.cond_value )
55
- {
56
- irep_idt property_id;
57
-
58
- if (step.pc ->is_assert ())
59
- property_id=source_location.get_property_id ();
60
- else if (step.pc ->is_goto ()) // unwinding, we suspect
61
- {
62
- property_id=
63
- id2string (step.pc ->source_location .get_function ())+
64
- " .unwind." +std::to_string (step.pc ->loop_number );
65
- }
66
-
67
- json_objectt &json_failure=dest_array.push_back ().make_object ();
68
-
69
- json_failure[" stepType" ]=json_stringt (" failure" );
70
- json_failure[" hidden" ]=jsont::json_boolean (step.hidden );
71
- json_failure[" internal" ]=jsont::json_boolean (step.internal );
72
- json_failure[" thread" ]=json_numbert (std::to_string (step.thread_nr ));
73
- json_failure[" reason" ]=json_stringt (id2string (step.comment ));
74
- json_failure[" property" ]=json_stringt (id2string (property_id));
75
-
76
- if (!json_location.is_null ())
77
- json_failure[" sourceLocation" ]=json_location;
78
- }
79
- break ;
80
-
81
- case goto_trace_stept::typet::ASSIGNMENT:
82
- case goto_trace_stept::typet::DECL:
83
- {
84
- irep_idt identifier=step.lhs_object .get_identifier ();
85
- json_objectt &json_assignment=dest_array.push_back ().make_object ();
86
-
87
- json_assignment[" stepType" ]=json_stringt (" assignment" );
88
-
89
- if (!json_location.is_null ())
90
- json_assignment[" sourceLocation" ]=json_location;
91
-
92
- std::string value_string, binary_string, type_string, full_lhs_string;
93
- json_objectt full_lhs_value;
94
-
95
- DATA_INVARIANT (
96
- step.full_lhs .is_not_nil (),
97
- " full_lhs in assignment must not be nil" );
98
- exprt simplified=simplify_expr (step.full_lhs , ns);
99
-
100
- if (trace_options.json_full_lhs )
101
- {
102
- class comment_base_name_visitort : public expr_visitort
103
- {
104
- private:
105
- const namespacet &ns;
106
-
107
- public:
108
- explicit comment_base_name_visitort (const namespacet &ns) : ns(ns)
109
- {
110
- }
111
-
112
- void operator ()(exprt &expr) override
113
- {
114
- if (expr.id () == ID_symbol)
115
- {
116
- const symbolt &symbol = ns.lookup (expr.get (ID_identifier));
117
- if (expr.find (ID_C_base_name).is_not_nil ())
118
- INVARIANT (
119
- expr.find (ID_C_base_name).id () == symbol.base_name ,
120
- " base_name comment does not match symbol's base_name" );
121
- else
122
- expr.add (ID_C_base_name, irept (symbol.base_name ));
123
- }
124
- }
125
- };
126
- comment_base_name_visitort comment_base_name_visitor (ns);
127
- simplified.visit (comment_base_name_visitor);
128
- }
129
-
130
- full_lhs_string=from_expr (ns, identifier, simplified);
131
-
132
- const symbolt *symbol;
133
- irep_idt base_name, display_name;
134
-
135
- if (!ns.lookup (identifier, symbol))
136
- {
137
- base_name=symbol->base_name ;
138
- display_name=symbol->display_name ();
139
- if (type_string==" " )
140
- type_string=from_type (ns, identifier, symbol->type );
141
-
142
- json_assignment[" mode" ]=json_stringt (id2string (symbol->mode ));
143
- exprt simplified=simplify_expr (step.full_lhs_value , ns);
144
-
145
- full_lhs_value=json (simplified, ns, symbol->mode );
146
- }
147
- else
148
- {
149
- DATA_INVARIANT (
150
- step.full_lhs_value .is_not_nil (),
151
- " full_lhs_value in assignment must not be nil" );
152
- full_lhs_value=json (step.full_lhs_value , ns, ID_unknown);
153
- }
154
-
155
- json_assignment[" value" ]=full_lhs_value;
156
- json_assignment[" lhs" ]=json_stringt (full_lhs_string);
157
- if (trace_options.json_full_lhs )
158
- {
159
- // Not language specific, still mangled, fully-qualified name of lhs
160
- json_assignment[" rawLhs" ] =
161
- json_irept (true ).convert_from_irep (simplified);
162
- }
163
- json_assignment[" hidden" ]=jsont::json_boolean (step.hidden );
164
- json_assignment[" internal" ]=jsont::json_boolean (step.internal );
165
- json_assignment[" thread" ]=json_numbert (std::to_string (step.thread_nr ));
166
-
167
- json_assignment[" assignmentType" ]=
168
- json_stringt (
169
- step.assignment_type ==
170
- goto_trace_stept::assignment_typet::ACTUAL_PARAMETER?
171
- " actual-parameter" :
172
- " variable" );
173
- }
174
- break ;
175
-
176
- case goto_trace_stept::typet::OUTPUT:
177
- {
178
- json_objectt &json_output=dest_array.push_back ().make_object ();
179
-
180
- json_output[" stepType" ]=json_stringt (" output" );
181
- json_output[" hidden" ]=jsont::json_boolean (step.hidden );
182
- json_output[" internal" ]=jsont::json_boolean (step.internal );
183
- json_output[" thread" ]=json_numbert (std::to_string (step.thread_nr ));
184
- json_output[" outputID" ]=json_stringt (id2string (step.io_id ));
185
-
186
- // Recovering the mode from the function
187
- irep_idt mode;
188
- const symbolt *function_name;
189
- if (ns.lookup (source_location.get_function (), function_name))
190
- // Failed to find symbol
191
- mode=ID_unknown;
192
- else
193
- mode=function_name->mode ;
194
- json_output[" mode" ]=json_stringt (id2string (mode));
195
- json_arrayt &json_values=json_output[" values" ].make_array ();
196
-
197
- for (const auto &arg : step.io_args )
198
- {
199
- if (arg.is_nil ())
200
- json_values.push_back (json_stringt (" " ));
201
- else
202
- json_values.push_back (json (arg, ns, mode));
203
- }
204
-
205
- if (!json_location.is_null ())
206
- json_output[" sourceLocation" ]=json_location;
207
- }
208
- break ;
209
-
210
- case goto_trace_stept::typet::INPUT:
211
- {
212
- json_objectt &json_input=dest_array.push_back ().make_object ();
213
-
214
- json_input[" stepType" ]=json_stringt (" input" );
215
- json_input[" hidden" ]=jsont::json_boolean (step.hidden );
216
- json_input[" internal" ]=jsont::json_boolean (step.internal );
217
- json_input[" thread" ]=json_numbert (std::to_string (step.thread_nr ));
218
- json_input[" inputID" ]=json_stringt (id2string (step.io_id ));
219
-
220
- // Recovering the mode from the function
221
- irep_idt mode;
222
- const symbolt *function_name;
223
- if (ns.lookup (source_location.get_function (), function_name))
224
- // Failed to find symbol
225
- mode=ID_unknown;
226
- else
227
- mode=function_name->mode ;
228
- json_input[" mode" ]=json_stringt (id2string (mode));
229
- json_arrayt &json_values=json_input[" values" ].make_array ();
230
-
231
- for (const auto &arg : step.io_args )
232
- {
233
- if (arg.is_nil ())
234
- json_values.push_back (json_stringt (" " ));
235
- else
236
- json_values.push_back (json (arg, ns, mode));
237
- }
238
-
239
- if (!json_location.is_null ())
240
- json_input[" sourceLocation" ]=json_location;
241
- }
242
- break ;
243
-
244
- case goto_trace_stept::typet::FUNCTION_CALL:
245
- case goto_trace_stept::typet::FUNCTION_RETURN:
246
- {
247
- std::string tag=
248
- (step.type ==goto_trace_stept::typet::FUNCTION_CALL)?
249
- " function-call" :" function-return" ;
250
- json_objectt &json_call_return=dest_array.push_back ().make_object ();
251
-
252
- json_call_return[" stepType" ]=json_stringt (tag);
253
- json_call_return[" hidden" ]=jsont::json_boolean (step.hidden );
254
- json_call_return[" internal" ]=jsont::json_boolean (step.internal );
255
- json_call_return[" thread" ]=json_numbert (std::to_string (step.thread_nr ));
256
-
257
- const symbolt &symbol=ns.lookup (step.identifier );
258
- json_objectt &json_function=json_call_return[" function" ].make_object ();
259
- json_function[" displayName" ]=
260
- json_stringt (id2string (symbol.display_name ()));
261
- json_function[" identifier" ]=json_stringt (id2string (step.identifier ));
262
- json_function[" sourceLocation" ]=json (symbol.location );
263
-
264
- if (!json_location.is_null ())
265
- json_call_return[" sourceLocation" ]=json_location;
266
- }
267
- break ;
268
-
269
- default :
270
- if (source_location!=previous_source_location)
271
- {
272
- // just the source location
273
- if (!json_location.is_null ())
274
- {
275
- json_objectt &json_location_only=dest_array.push_back ().make_object ();
276
- json_location_only[" stepType" ]=json_stringt (" location-only" );
277
- json_location_only[" hidden" ]=jsont::json_boolean (step.hidden );
278
- json_location_only[" internal" ]=jsont::json_boolean (step.internal );
279
- json_location_only[" thread" ]=
280
- json_numbert (std::to_string (step.thread_nr ));
281
- json_location_only[" sourceLocation" ]=json_location;
282
- }
283
- }
284
- }
285
-
286
- if (source_location.is_not_nil () && source_location.get_file ()!=" " )
287
- previous_source_location=source_location;
288
- }
289
- }
0 commit comments