Skip to content

Commit 879cb13

Browse files
authored
Merge pull request #4011 from tautschnig/json_expr-cleanup
Cleanup expression-to-json
2 parents 8615e3b + 0667499 commit 879cb13

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

src/goto-programs/json_expr.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ static exprt simplify_json_expr(const exprt &src)
9393
/// The `mode` argument is used to correctly report types.
9494
/// \param type: a type
9595
/// \param ns: a namespace
96-
/// \param mode: language in which the code was written; for now ID_C and
97-
/// ID_java are supported
96+
/// \param mode: language in which the code was written
9897
/// \return a json object
9998
json_objectt json(const typet &type, const namespacet &ns, const irep_idt &mode)
10099
{
@@ -177,6 +176,10 @@ json_objectt json(const typet &type, const namespacet &ns, const irep_idt &mode)
177176
members.push_back(std::move(e));
178177
}
179178
}
179+
else if(type.id() == ID_struct_tag)
180+
{
181+
return json(ns.follow_tag(to_struct_tag_type(type)), ns, mode);
182+
}
180183
else if(type.id() == ID_union)
181184
{
182185
result["name"] = json_stringt("union");
@@ -190,6 +193,10 @@ json_objectt json(const typet &type, const namespacet &ns, const irep_idt &mode)
190193
members.push_back(std::move(e));
191194
}
192195
}
196+
else if(type.id() == ID_union_tag)
197+
{
198+
return json(ns.follow_tag(to_union_tag_type(type)), ns, mode);
199+
}
193200
else
194201
result["name"] = json_stringt("unknown");
195202

@@ -207,26 +214,23 @@ static std::string binary(const constant_exprt &src)
207214
/// The mode is used to correctly report types.
208215
/// \param expr: an expression
209216
/// \param ns: a namespace
210-
/// \param mode: language in which the code was written; for now ID_C and
211-
/// ID_java are supported
217+
/// \param mode: language in which the code was written
212218
/// \return a json object
213219
json_objectt json(const exprt &expr, const namespacet &ns, const irep_idt &mode)
214220
{
215221
json_objectt result;
216222

217-
const typet &type = ns.follow(expr.type());
218-
219223
if(expr.id() == ID_constant)
220224
{
225+
const constant_exprt &constant_expr = to_constant_expr(expr);
226+
227+
const typet &type = expr.type();
228+
221229
std::unique_ptr<languaget> lang;
222-
if(mode == ID_unknown)
223-
lang = std::unique_ptr<languaget>(get_default_language());
224-
else
225-
{
230+
if(mode != ID_unknown)
226231
lang = std::unique_ptr<languaget>(get_language_from_mode(mode));
227-
if(!lang)
228-
lang = std::unique_ptr<languaget>(get_default_language());
229-
}
232+
if(!lang)
233+
lang = std::unique_ptr<languaget>(get_default_language());
230234

231235
const typet &underlying_type =
232236
type.id() == ID_c_bit_field ? to_c_bit_field_type(type).subtype() : type;
@@ -238,10 +242,9 @@ json_objectt json(const exprt &expr, const namespacet &ns, const irep_idt &mode)
238242
std::string value_string;
239243
lang->from_expr(expr, value_string, ns);
240244

241-
const constant_exprt &constant_expr = to_constant_expr(expr);
242245
if(
243246
type.id() == ID_unsignedbv || type.id() == ID_signedbv ||
244-
type.id() == ID_c_bit_field)
247+
type.id() == ID_c_bit_field || type.id() == ID_c_bool)
245248
{
246249
std::size_t width = to_bitvector_type(type).get_width();
247250

@@ -320,15 +323,6 @@ json_objectt json(const exprt &expr, const namespacet &ns, const irep_idt &mode)
320323
result["binary"] = json_stringt(expr.is_true() ? "1" : "0");
321324
result["data"] = jsont::json_boolean(expr.is_true());
322325
}
323-
else if(type.id() == ID_c_bool)
324-
{
325-
result["name"] = json_stringt("integer");
326-
result["width"] =
327-
json_numbert(std::to_string(to_bitvector_type(type).get_width()));
328-
result["type"] = json_stringt(type_string);
329-
result["binary"] = json_stringt(expr.get_string(ID_value));
330-
result["data"] = json_stringt(value_string);
331-
}
332326
else if(type.id() == ID_string)
333327
{
334328
result["name"] = json_stringt("string");
@@ -358,6 +352,8 @@ json_objectt json(const exprt &expr, const namespacet &ns, const irep_idt &mode)
358352
{
359353
result["name"] = json_stringt("struct");
360354

355+
const typet &type = ns.follow(expr.type());
356+
361357
// these are expected to have a struct type
362358
if(type.id() == ID_struct)
363359
{

0 commit comments

Comments
 (0)