Skip to content

Port JSON pointer output fix #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 68 additions & 2 deletions src/util/json_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,67 @@ Author: Peter Schrammel
#include "fixedbv.h"
#include "std_expr.h"
#include "config.h"
#include "identifier.h"

#include "json_expr.h"

/*******************************************************************\

Function: simplify_json_expr

Inputs:

Outputs:

Purpose:

\*******************************************************************/

static exprt simplify_json_expr(
const exprt &src,
const namespacet &ns)
{
if(src.id()==ID_constant)
{
const typet &type=ns.follow(src.type());

if(type.id()==ID_pointer)
{
const irep_idt &value=to_constant_expr(src).get_value();

if(value!=ID_NULL &&
(value!=std::string(value.size(), '0') ||
!config.ansi_c.NULL_is_zero) &&
src.operands().size()==1 &&
src.op0().id()!=ID_constant)
// try to simplify the constant pointer
return simplify_json_expr(src.op0(), ns);
}
}
else if(src.id()==ID_address_of &&
src.operands().size()==1 &&
src.op0().id()==ID_member &&
id2string(to_member_expr(
src.op0()).get_component_name()).find("@")!=std::string::npos)
{
// simplify things of the form &member_expr(object, @class_identifier)
return simplify_json_expr(src.op0(), ns);
}
else if(src.id()==ID_member &&
src.operands().size()==1 &&
id2string(
to_member_expr(src).get_component_name())
.find("@")!=std::string::npos)
{
// simplify things of the form member_expr(object, @class_identifier)
return simplify_json_expr(src.op0(), ns);
}

return src;
}

/*******************************************************************\

Function: json

Inputs:
Expand Down Expand Up @@ -262,9 +318,19 @@ json_objectt json(
else if(type.id()==ID_pointer)
{
result["name"]=json_stringt("pointer");
result["binary"]=json_stringt(expr.get_string(ID_value));
if(expr.get(ID_value)==ID_NULL)
exprt simpl_expr=simplify_json_expr(expr, ns);
if(simpl_expr.get(ID_value)==ID_NULL ||
// remove typecast on NULL
(simpl_expr.id()==ID_constant && simpl_expr.type().id()==ID_pointer &&
simpl_expr.op0().get(ID_value)==ID_NULL))
result["data"]=json_stringt("NULL");
else if(simpl_expr.id()==ID_symbol)
{
const irep_idt &ptr_id=to_symbol_expr(simpl_expr).get_identifier();
identifiert identifier(id2string(ptr_id));
assert(!identifier.components.empty());
result["data"]=json_stringt(identifier.components.back());
}
}
else if(type.id()==ID_bool)
{
Expand Down